Monday 20 May 2013

_Just _Get _Over _It (aka _Tuples _1_0_1)

Coming from Java, the frequent use of ‘_’ freaks me out.  E.g. when accessing Tuple members

In this case I know (from the Fast Track to Scala training) that under the covers we have a Tuple2 object, with methods called ‘._1’ and ‘._2’.  (You can see this if you use the REPL / SBT console, and try TAB-completion on the newly created “tuple” object.)

I’m resisting the urge to call the equivalent of “get1()” and “get2()” in Java as I know that’s not really embracing the Scala way of doing things.  I’m trying really hard to move towards the light, really I am.

I’m also trying to get my head round the “(“apple”, “dog”) syntax. This seems to be a Tuple-literal, but I’ll need to look into it.  What I do know is that this passes:

And this does too (perhaps less surprisingly):

Bit of a conceptual leap from the Koans there…

 

Specially Selected Bonus Prize

A little bit of additional info for the keen amongst us. Scala By Example he say:

“Tuples are so convenient that Scala defines special syntax for them. To form a tuple with n elements x1, . . . , xn one can write (x1, . . . , xn). This is equivalent to Tuplen(x1, . . . , xn).”  Now why couldn’t they have used that ‘…’ goodness in Ranges too?

No comments:

Post a Comment