jastrachan | 22 Feb 18:16 2005
Picon

Re: support of Java style arrays; or using Groovy arrays?

On 22 Feb 2005, at 15:53, LARSON, BRIAN (SBCSI) wrote:
> I've never liked the Java array initializer syntax.  I find it
> unintuitive and extremely noisy.  This is coming from someone who
> supports mandatory parens and semis. :)

LOL!

Me too actually; I've often found the restrictions on the Java syntax 
strange. e.g. it can handle declarations, but not assignments...

int[] x = {1, 2, 3}

// not
x = {2, 3}

> Also, I generally like one syntax for doing things.  So, I would agree
> that we should not keep the Java syntax AND a new syntax.
>
> I think the [1,2,3] syntax reads nicely, but I'm a little worried that
> it could conflict in a few situations.  Do we always want to
> automatically coerce from a list to an Array?

Note that the grammar/AST could detect common cases like...

def x = [1, 2, 3] as int[]

and just create an int[] directly.

So it may look like a list is made and then coerced into an array; but 
we should short circuit this in the parser.

>  Confusing the two might
> cause problems such as lists or arrays not being the type you expected.
> For example (obviously contrived):
> Class Test {
>  String test(int val) {
>    println val
>  }
>  String test(List myList) {
>    return myList.size
>  }
>  void testit() {
>   def test = [1,2,3]

BTW should this line be

     def x = [1, 2, 3]

?

>   println x.size
>   println x[0..1]
>   println(x.get(0))
>   def val = test [0]
>   println val
>  }
> }
> Maybe this is partially my concern about optional parens:  def val =
> test [0]
> In this case, is it calling the test method with a list containing 0 or
> is it referring to the test array/list at index 0.

Agreed. Thats actually nothing to do with lists v arrays; its more to 
do with lists v optional parens
As it happens it'll try get the property 'test' and then index into it. 
The above generates a wierd error in the current runtime, as we are 
still exposing the 'method pointer' logic. If that was fixed, we'd get 
a 'no such property 'test'' exception. To pass in a list its

def val = test([0])

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


Gmane