1 > 0 true greater than (a comparison operation)
1 == 0 false equals to (another comparision operation)
1.max(0) 1 max operation returns the greater value between the left and right argument
-5.abs 5 absolute value operation
"Hello, " + "world!" "Hello, world!" concatentation operation
"#" * 3 "###" times operation
"Alice".toUpperCase "ALICE" to upper case operation
true && true true logical and operation with infix notation
true.&&(true) true logical and operator with dot notatation
x _op_ y
is x.op(y)
x op
is x.op()
x(i) = x(j)
is x.update(i, x.apply(j))
++
or --
for numbers. Use x += 1
and y -= 1
x == y
to compare object – it calls equals
myObject myMethod 1
is the same as calling myObject.myMethod(1)
Operator (i.e. function) names can be alphanumeric, symbolic (e.g. x1
, *
, +?%&
, vector_++
, counter_=
)
1 + 2 * 3
1.+(2.*(3))
1 + 2 // Int 1 + 2.0 // Double
## Operator Precedence in General
The precedence of an operator is determined by its first character, with the following increasing order of priority:
Assigment Operations (lowest priority)
Non-operator characters (all letters)
|
^
&
= !
< >
:
+ -
* / %
All other special characters (highest priority)
## Operation Associativity
The associativity of an operator is determined by its last character: Right-associative if ending with `:`, Left-associative otherwise.
Note that assignment operators have lowest precedence. (Read Scala Language Specification 2.9 sections 6.12.3, 6.12.4 for more info)
## Defining your own Operators:
* Methods with one parameter can be called with `infix` notation
```scala
def likes(movie: String): Boolean = movie == favoriteMovie
mary likes "Inception"
prefix
notation
def unary_! : String = s"$name, what the heck?!?"
!mary
postfix
notation
def isAlive: Boolean = true
mary isAlive
// the apply function
def apply(): String = s"Hi, my name is $name and I like $favoriteMovie"
mary() // will call the apply function