Most operators in Droid can be implemented on user-defined types. It is simply a matter of implementing the method that corresponds to the operator. There are, however, some that do something which cannot be done via methods, and as such they cannot be redefined for user-defined types.
Precedence |
Syntax |
Expansion |
|
Lowest |
|
x.or { y } |
|
Higher |
|
x.and { y } |
|
Higher |
|
x.equal(y) |
|
(same) |
|
!(x == y) |
|
(same) |
|
x.less(y) |
|
(same) |
|
x < y || x == y |
|
(same) |
|
!(x <= y) |
|
(same) |
|
!(x < y) |
|
Higher |
|
y.contains(x) |
|
Higher |
|
x.concatenate(y) |
|
(same) |
|
x.toString() @ y.toString() |
|
Higher |
|
x.plus(y) |
|
(same) |
|
x.minus(y) |
|
Higher |
|
x.times(y) |
|
(same) |
|
x.over(y) |
|
Higher |
|
x.not() |
|
(same) |
|
x.negate() |
|
Highest |
|
x.power(y) |
Syntax |
Description |
|
Assigns a new value |
|
Shorthand for |
|
Fetches a method |
|
Calls |
|
Calls |
|
Calls |
|
Calls |
|
Returns |
|
Repeats |
|
Returns a list of |
Calls have the highest precedence, and the other of these operators have the lowest precedence.
Note that plain vanilla calls that only take one argument which is an anonymous function don't need parenthesis:
val twice = list.Map {|n: Int| n * 2 }
Inside curly braces, you can have a sequence of expressions { e1; e2; ... en }
. The n-1 first expressions are evaluated in order, and their result is discarded (only side effects are kept). Then the last expression is evaluated and becomes the result of the sequence. If n is zero, there are no side effects and the result is Void
.
Syntactically, the semicolons are not necessary if you use line breaks. A line break after a prefix or infix operator, or after a beginning bracket or parenthesis will not be confused with a sequence separator, but a line break after anything else will work just like a semicolon. You are allowed a semicolon just before the ending curly brace too, which will be ignored.