Droid2Operators

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.

The Usual Operators

Precedence

Syntax

Expansion

Lowest

x || y

x.or { y }

Higher

x && y

x.and { y }

Higher

x == y

x.equal(y)

(same)

x != y

!(x == y)

(same)

x < y

x.less(y)

(same)

x <= y

x < y || x == y

(same)

x > y

!(x <= y)

(same)

x >= y

!(x < y)

Higher

x in y

y.contains(x)

Higher

x @ y

x.concatenate(y)

(same)

x ~ y

x.toString() @ y.toString()

Higher

x + y

x.plus(y)

(same)

x - y

x.minus(y)

Higher

x * y

x.times(y)

(same)

x / y

x.over(y)

Higher

!x

x.not()

(same)

-x

x.negate()

Highest

x ** y

x.power(y)

Operators of Special Interest

Syntax

Description

x = y

Assigns a new value y to the variable x (must be declared with the var keyword).

x += y

Shorthand for x = x + y (also works for the operators - * / @ ~ ).

x.y

Fetches a method y from the instace x (use parenthesis to call).

x.Property

Calls x.getProperty() where "Property" can be any camel case name.

x.Property = y

Calls x.setProperty(y) where "Property" can be any camel case name.

x(y1, y2, ...)

Calls x.get(y1, y2, ...) (if x is a raw method, it is called directly).

x(i1, i2, ...) = y

Calls x.set(i1, i2, ..., y) .

if(c) { t } else { e }

Returns t if c is true, and e otherwise (else if works too).

while(c) { e }

Repeats e as long as c remains true (zero or more times).

for(e in l where c) { v }

Returns a list of v for all the items in l where c is true (where c is optional).

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 }

Sequences

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.