18 Feb 2005 12:55
Re: On optional parentheses (again, sorry)
:) On 18 Feb 2005, at 11:44, Russel Winder wrote: > I had an idle moment so I looked back at one of my more trivial > scripts: > > flob = new ArrayList ( ) > flob.add ( "hello" ) > println ( flob.get ( 0 ) ) > println ( flob.size ( ) ) > > Did I mention it was trivial?> > Anyway I removed all the parentheses: > > adob = new ArrayList ( ) # Parentheses required here
> adob.add "hello" > println adob.get 0 > println flob.size > > and it ran exactly the same except for the last line which in current > Groovy is a reference to a method / closure so reported an address > rather than the value 1. But it ran. FWIW in New Groovy it doesn't. class RussellsOptionalParenTest extends GroovyTestCase { void testMethodCallWithOneParam() { adob = new ArrayList() adob.add "hello" println adob.get 0 println flob.size } } generates... /workspace/groovy/groovy-core/src/test-new/groovy/bugs/ RussellsOptionalParenTest.groovy:6:26: unexpected token: 0 i.e. you can only omit parens on top level statements (the println). After I fixed the 2 typeos (no such 'flob' and flob.size currently returns the method closure rather than generating a no such property) so this version... class RussellsOptionalParenTest extends GroovyTestCase { void testMethodCallWithOneParam() { adob = new ArrayList() adob.add "hello" println adob.get(0) println adob.size() } } it output... .hello 1 Time: 0.049 OK (1 test) > I have in the past argued for the former and on the grounds of Javaness > will probably stick with that but I could live with the latter > remaining > legal Groovy. My question is will it be in new Groovy? If so can we > get rid of the "( )" after the name of the type to create a new object > of? I'd be tempted to say, not for zero argument constructors, for the same reasons as method calls with zero arguments. Also new is quite heavily overloaded with paremeters, arrays, inner classes & closures, so we need to be very careful about making things optional. So far you can only omit parens on top level method calls. James ------- http://radio.weblogs.com/0112098/
>
> Anyway I removed all the parentheses:
>
> adob = new ArrayList ( ) # Parentheses required here
> adob.add "hello"
> println adob.get 0
> println flob.size
>
> and it ran exactly the same except for the last line which in current
> Groovy is a reference to a method / closure so reported an address
> rather than the value 1. But it ran.
FWIW in New Groovy it doesn't.
class RussellsOptionalParenTest extends GroovyTestCase {
void testMethodCallWithOneParam() {
adob = new ArrayList()
adob.add "hello"
println adob.get 0
println flob.size
}
}
generates...
/workspace/groovy/groovy-core/src/test-new/groovy/bugs/
RussellsOptionalParenTest.groovy:6:26: unexpected token: 0
i.e. you can only omit parens on top level statements (the println).
After I fixed the 2 typeos (no such 'flob' and flob.size currently
returns the method closure rather than generating a no such property)
so this version...
class RussellsOptionalParenTest extends GroovyTestCase {
void testMethodCallWithOneParam() {
adob = new ArrayList()
adob.add "hello"
println adob.get(0)
println adob.size()
}
}
it output...
.hello
1
Time: 0.049
OK (1 test)
> I have in the past argued for the former and on the grounds of Javaness
> will probably stick with that but I could live with the latter
> remaining
> legal Groovy. My question is will it be in new Groovy? If so can we
> get rid of the "( )" after the name of the type to create a new object
> of?
I'd be tempted to say, not for zero argument constructors, for the same
reasons as method calls with zero arguments. Also new is quite heavily
overloaded with paremeters, arrays, inner classes & closures, so we
need to be very careful about making things optional. So far you can
only omit parens on top level method calls.
James
-------
RSS Feed