A A A i

mercredi 10 décembre 2008mercredi 10 décembre 2008

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

Commentaires ici et ailleurs :

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 #


qui est

nom : Damien Bonvillain
courriel : kame à cinemasie.com
bloggercode:
B9 D+ T+ K S F I- O X+ E- L- C-- Y1 R+ W- P+ M5 N-- N+
un peu plus : Google Whoring tortue

Messagerie instantanée

    les koms

    m'enfin

    Quant à mes invectives imaginaires, je vous laisse chercher un endroit adéquat pour les ranger. Elles craignent la lumière, si vous voulez une piste.
    Lien associé
    Eolas - « Aimez moi, c'est un ordre. »

    les bons vieux


    archives

    « décembre 2008 »
    lunmarmerjeuvensamdim
    1234567
    891011121314
    15161718192021
    22232425262728
    293031

    XML RSS 2.0 XML RSS 2.0 commentaires A A A i

    liens

    allégeance

    Blog sans chat

    colophon

    Propulsé par pointClairMerci à la caféineDevelopment with EmacsBadges from GTMcKnightFreeListed on BlogSharesGeoURL