Groovy Magic
groovy> a = '4' groovy> b = '5' groovy> println a + b 45 groovy> println a.asType(int) + b.asType(int) 105 groovy> // WTF? groovy> println a.asType(int) 52 groovy> println b.asType(int) 53 groovy> // leave long and prosper ASCII groovy> println a as int + b as int expecting EOF, found '+' groovy> println (a as int) + (b as int) Cannot invoke method plus() on null object groovy> a = a as int groovy> b = b as int groovy> println a + b 9 groovy> // w00t!
(Cyberpunk, 2008/12/10 18:58) lien permanent

Wizard Bonvillain.
Posté par Tweety, le mercredi 10 décembre 2008 à 23:00 #
groovy> println (a as int) + (b as int)
Cannot invoke method plus() on null object
That happened because println is nothing special, just a method call. So it has a higher priority than the plus, i.e.
println((a as int) + (b as int))
works. If you are not sure about operator preferences, you can always use paranthesis.
Posté par Nils Kassube, le jeudi 11 décembre 2008 à 02:07 #
And you criticized ruby
You decided to put a bit of exotism in your work?
Posté par Florent, le jeudi 11 décembre 2008 à 11:50 #
Thanks, nice to know that println isn't handled separately. So now the question is, from a grammar point of view, does "println((a as int) + (b as int))" mean:
- println called with (a as int) + (b as int) as a parameter and the optionnal parenthesis used
or
- println called with ((a as int) + (b as int)) with the optionnal parenthesis left off?
"If you are not sure about operator preferences, you can always use paranthesis."
I know my operator preferences, I didn't know that Groovy chose the path of C# ambiguation.
Posté par Damien B, le jeudi 11 décembre 2008 à 11:54 #
@Florent: forced by a lazy customer
Posté par Damien B, le jeudi 11 décembre 2008 à 11:57 #
They all are
Posté par Florent, le dimanche 14 décembre 2008 à 11:35 #