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 ...

Get Scala for the Impatient now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.