O'Reilly logo

Scala for the Impatient by Cay S. Horstmann

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Operators

• Infix notation x op y is x.op(y), postfix notation x op is x.op()

• Only + - ! ~ can be prefix—define method unary_op

• Assignment xop=y is x = xop y unless defined separately

• Precedence depends on first character, except for assignments

image

• Right associative if last character is a colon :

x(i) = x(j) is x.update(i, x.apply(j))

• There is no ++ or -- for numbers. Use x += 1; y -= 1

• Use x == y to compare objects—it calls equals

Functions

def triple(x: Int) = 3 * x // Parameter name: Typeval f = (x: Int) => 3 * x // Anonymous function(1 to 10).map(3 * _) // Function with anonymous parameterdef greet(x: Int) { // Without =, return ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required