Scala Wonderland #1: All operations are method calls

I’ve started learning Scala two months ago and I can’t get back to Murakami’s 1Q84 so exciting it is. In the coming series I’d like to share my excitement with you. It won’t be yet another step-by-step tutorial. I’ll share features that made me think (IBM should be proud of me). For whatever reason.

val i = 1 + 2

… is equal to …

val i = (1).+(2)

Scala doesn’t have operators. It has methods with names like +, -, *, / instead. The code above invokes method named “+” on the object Int representing number 1 passing Int object of value 2 as an argument.

Note that you don’t have to type “.” to ivoke a method on an object and also you don’t need to provide parentheses around method arguments. This knowledge helps a lot when trying to understand what is a program written in Scala actually doing.

Stay tuned!

Leave a Reply