Groovy compiler checks

View: New views
4 Messages — Rating Filter:   Alert me  

Groovy compiler checks

by Paolo DiCanio :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

In Groovy types are optional. When I started using the language I thought that this meant that if I define a method such as

    String sayHello(String arg) { return "hello $arg" }

Then the compiler would raise an error if I tried to call it with an argument that is not a String or try to assign the the result to a variable that is not of type String (or parent thereof).

Of course, I quickly discovered that this is not the case - these checks are only made at runtime. How difficult would it be to perform these checks at compile-time? Do people even agree that moving these checks to compile-time would be a good idea? I realise that it's more difficult to do these kinds of checks in a dynamic language, but if for example I were to call:

    println sayHello(new Object())

Then even in Groovy, I think that's guaranteed to be a type error?

I should emphasise that I'm not suggesting Groovy should become a statically-typed language, if I want a such a language there are already plenty of those available (Scala, Java). I'm simply suggesting that when I do provide type information, I'd like them to be checked as early as possible.

Cheers,
Paolo

Re: Groovy compiler checks

by Jochen Theodorou :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Paolo DiCanio schrieb:
> Do people even agree that moving these checks to compile-time
> would be a good idea? I realise that it's more difficult to do these kinds
> of checks in a dynamic language, but if for example I were to call:
>
>     println sayHello(new Object())
>
> Then even in Groovy, I think that's guaranteed to be a type error?

only if there is no sayHello(Object) method. And this method might be
added at runtime, so there is no way for us to know at compiler time
that this is really the case

bye blackdrag

--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Groovy compiler checks

by Robert Fischer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

How would the  you differentiate that from this case?

// In some far-flung set-up code (e.g. a Grails plugin's doWithDynamicMethods closure)
Foo.metaClass.sayHello = { return "hello $it" }

// In Foo.groovy
class Foo {
   String sayHello(String arg) { return "hello $arg" }
   void doGreet() { println sayHello(new Object()) }  // Is this a compile error?  It's valid code.
}

~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Grails Expert Retainer Services
http://smokejumperit.com/grails-retainer/


Paolo DiCanio wrote:

> Hi,
>
> In Groovy types are optional. When I started using the language I thought
> that this meant that if I define a method such as
>
>     String sayHello(String arg) { return "hello $arg" }
>
> Then the compiler would raise an error if I tried to call it with an
> argument that is not a String or try to assign the the result to a variable
> that is not of type String (or parent thereof).
>
> Of course, I quickly discovered that this is not the case - these checks are
> only made at runtime. How difficult would it be to perform these checks at
> compile-time? Do people even agree that moving these checks to compile-time
> would be a good idea? I realise that it's more difficult to do these kinds
> of checks in a dynamic language, but if for example I were to call:
>
>     println sayHello(new Object())
>
> Then even in Groovy, I think that's guaranteed to be a type error?
>
> I should emphasise that I'm not suggesting Groovy should become a
> statically-typed language, if I want a such a language there are already
> plenty of those available (Scala, Java). I'm simply suggesting that when I
> do provide type information, I'd like them to be checked as early as
> possible.
>
> Cheers,
> Paolo

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Groovy compiler checks

by Chris Broadfoot :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

As a corollary, here's an interesting blog post on the matter-

http://james-iry.blogspot.com/2009/07/groovy-does-not-have-optional-static.html

Especially, read the comments.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email