Skip to main content
Version: Next

Operators

An operator is a reserved symbol used to compute value(s) and return a result. There are three kinds of operators: unary, binary and ternary.

  • Unary operators are used with only one expression: -2, where - is the unary operator used to set compatible numeric value negative.
  • Binary ones are used with two expressions: 1 + 2.
  • Ternary are the rarest ones, at this moment, only the Drift Conditional are ternary operators: a == b ? 1 : 0, where ? and : are both ternary operators.

The expressions used with an operator are called operands.

Assignment

It is one of the most used operators. The assignment permits defining a new variable or assigning a new value to an existing variable.

Variable declaration:

let immutableVar = 10
var mutableVar = 5

Variable reassignment:

var a = 10
a = 5
// a value is no longer 10, and becomes 5

Named parameter:

fun say(message) {
print(message)
}

say(message = "Hello, World!")
// Here, the '=' symbol assigns a value to a named parameter. Not required for positional ones.

Arithmetic

Drift implements standard arithmetic operators that can be used with numeric values:

  • Addition, using +
  • Subtraction, using -
  • Multiplication, using *
  • Division, using /
1 + 2   // equals 3
10 - 3 // equals 7
2 * 3 // equals 6
10 / 2 // equals 5

By default, Drift does not support operator overflow. Overflow type max/min value will throw an exception. In case of numeric type overflow without an explicit type, Drift will apply the most optimized type.

let a = 1000000000000000        // It overflows Int type, so Drift will apply Int64
let b: Int = 1000000000000000 // It overflows Int type, an exception will throw
let c: Int64 = 1000000000000000 // It does not overflow Int64 type

Overflow Operators

warning

At this moment, Drift does not implement overflow methods.

Special behaviors

Some arithmetic operators have special behaviors with precise types:

  • Multiplication operator * permits to multiply string concatenation
let name = "Bob"
let doubleName = name * 2 // equals "BobBob"
  • Addition operator + permits to concatenate strings
let name = "Bob"
let welcome = "Hello, " + name // equals "Hello, Bob"

Remainder Operator

The remainder is the value remaining from a Euclidean division, also called modulo operation.

Example:

11 % 5      // equals 1

On Euclidean division 11 by 5, it remains 1.

Example Modulo: 11 % 5

Operators Precedences

Drift follows basic operators priority.

Operator precedence defines how to order chained operations expressions.

5 + 1 * 3 / 4
// Priorities: 5 + ( ( 1 * 3 ) / 4 )

Adding parentheses around an operation will give it higher priority.

(5 + 1) * 3 / 4
// Parenthesis' priority is higher than multiplication

Operator Methods

warning

At this moment, Drift does not implement operators methods.

+- and -+ Operators

warning

At this moment, Drift does not implement these operators.

They will permit respectively to:

  • +-: increment two-length container's first element and decrement second one
  • -+: decrement two-length container's first element and increment second one