tugwilson | 9 May 17:15
Picon
Favicon

Re: Re: [groovy-user] dif between def obj = new MyObject() vs MyObject obj = new MyObject()


Peter Niederwieser wrote:
> 
> I think the main reason why Groovy allows type annotations is to ease the
> transition from Java. In the case of variable definitions, if the
> definition is immediately followed by an assignment, it is in my opinion
> more natural to omit the type. If there is no immediate assignment, the
> program might be easier to understand (for humans and IDEs) if the type is
> present.
> If you come from Java, it's perfectly fine to always specify the type. My
> personal convention is to include the type only if there is no immediate
> assignment. People with a strong background in dynamic languages will
> probably always omit the type.
> 

I believe the original motivation to allow optional typing was to allow
proper integration with Java. If you can declare the parameter and return
types of methods then you can easily and clearly subclass Java classes and
implement Java interfaces. Declaring typed properties make it easier for
Java classes to use the properties. At one point there was an intention to
use the declared type of a variable (rather than the actual type of the
variable's value) to select methods but that seems never to have been
implemented.

My general rule of thumb with Groovy is to make anything which is likely to
be seen from a Java program typed and declare everything else untyped. Like
all rules of thumb there are exceptions, of course!

John Wilson
--

-- 
View this message in context: http://www.nabble.com/dif-between-def-obj-%3D-new-MyObject%28%29-vs-MyObject-obj-%3D-new-MyObject%28%29-tp17031075p17150269.html
Sent from the groovy - user mailing list archive at Nabble.com.

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

    http://xircles.codehaus.org/manage_email


Gmane