Tutorial by Examples

In Scala operators (such as +, -, *, ++, etc.) are just methods. For instance, 1 + 2 can be written as 1.+(2). These sorts of methods are called 'infix operators'. This means custom methods can be defined on your own types, reusing these operators: class Matrix(rows: Int, cols: Int, val data: Seq[...
Unary operators can be defined by prepending the operator with unary_. Unary operators are limited to unary_+, unary_-, unary_! and unary_~: class Matrix(rows: Int, cols: Int, val data: Seq[Seq[Int]]){ def +(that: Matrix) = { val newData = for (r <- 0 until rows) yield for (c <...

Page 1 of 1