Advanced Operators
In addition to basic operators introduced in Basics Chapter, Drift implements advanced, like Bitwise, Overflow and custom ones.
Bitwise Operators
Bitwise NOT Operator
At this moment, Drift does not implement bitwise NOT operator.
~0b0011 // equals 0b1100
Bitwise AND Operator
At this moment, Drift does not implement bitwise AND operator.
0b0011 & 0b0110 // equals 0b0010
Bitwise OR Operator
At this moment, Drift does not implement bitwise OR operator.
0b0011 | 0b0110 // equals 0b0111
Bitwise XOR Operator
At this moment, Drift does not implement bitwise XOR operator.
0b0011 ^ 0b0110 // equals 0b0101
Bitwise Left and Right Shift Operators
At this moment, Drift does not implement bitwise left and right shift operators.
0b0011 << 1 // equals 0b0110
0b0011 << 2 // equals 0b1100
0b0011 >> 1 // equals 0b0001
0b0011 >> 2 // equals 0b0000
Overflow Operator
The default numeric overflow behavior is to throw an exception (Too long). This can be avoided by using the Overflow Operator as a prefix of the used operator.
At this moment, Drift does not implement the overflow operator.
Int Context: [INT MAX] + 1 // Will throw (too long)
Int Context: [INT MAX] &+ 1 // Equals 0
Operator Method
At this moment, Drift does not implement operator methods.
class User(name: String) : +Equatable, +Incrementable {
operator == { $left.name == $right.name }
// Last expression as return type is possible
operator != (l: User, r: User) {
return l.name != r.name
}
// Explicit parameters are possible
prefix operator ++ { ... }
// Prefix operator: ++obj
postfix operator -- { ... }
// Postfix operator: obj--
}
Operator methods require the implementation of their prefab.