jastrachan | 29 Nov 16:55 2004
Picon

Re: variable declaration syntax: use cases and thoughts


On 29 Nov 2004, at 15:48, John Wilson wrote:
> On 29 Nov 2004, at 15:14, jastrachan@... wrote:
>
>> Thats a separate thing, declaring varargs. I'd rather not tie varargs 
>> to Java arrays, which are pretty sucky at best :)
>>
>> For varargs we could use syntax from Java 5 such as ...
>>
>
> Yes, that notation (and the Python * notation) was proposed when you 
> made the decision to use Object[] type the last time this was 
> discussed ;)
>
> There is a slightly more general point here...
>
> In Old Groovy we don't mind that list and map contents are Objects 
> because we don't really care vary much about type. In New Groovy we 
> have a distinction between typed and untyped values. So, does a list 
> and a map contain things of type Object or an untyped value?

It depends on the variable you assign them to...

> I'd guess that they contain untyped values so that they support duck 
> typing.
>
>
> e.g.
>
> x = [1, 'x', ...]
>
> x[4].someOddMethod()		// this is OK at compile time as the value is 
> untyped
>
> but the underlying things which implement maps and lists are just 
> normal Java classes. So are they special cases or does new Groovy 
> treat any method which returns Object as returning an untyped value? 
> How does it treat a returned type of GroovyObject?

It depends on the type of the root object of the expression.

Map map = ...
map[foo].someOddMethod() // compiler error are we're statically typed

var map = ...
map[foo].someOddMethod() // no compile error as we're dynamically typed

if ever you're statically typed and want to go dynamic in the middle, 
just introduce a temporary

Map map = ...
var temp = map[foo]
temp.someOddMethod() // no compiler error are we're dynamically typed

James
-------
http://radio.weblogs.com/0112098/


Gmane