Basic Operators

🚧 kaoscript will allow to override operators.

arithmetic

OperatorMeaning
-exprNegative
expr - exprSubtraction
expr + exprAddition
expr * exprMultiplication
expr / exprDivision
expr /. exprQuotient division
expr % exprModulo
++varPre-increment
var++Post-increment
--varPre-decrement
var--Post-decrement
// Numbers
1 + 2 // 3
1 + 2 + 3 // 6
1 - 2 // -1
-2 // -2
2 * 3 // 6
18 / 3 // 6
18 / 5 // 3.6
18 /. 5 // 3
18 % 3 // 0
18 % 5 // 3
18 % 10 % 5 // 3
let x = 0
++x // 1
x++ // 2
--x // 1
x-- // 0
// Strings
'foo' + 'bar' // foobar

comparison

OperatorMeaning
expr == exprEqual
expr != exprNot equal
expr > exprGreater than
expr < exprLess than
expr >= exprGreater than or equal
expr <= exprLess than or equal
1 == 1 // true
1 == 1 == 2 // false
1 != 2 // true
3 > 2 > 1 // true
3 < 5 < 1 // false
3 <= 3 <= 5 // true
4 >= 1 // true

Chaining comparisons

Comparisons can be chained.

1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5
// true
if 1 < x() < 42 {
// `x()` is evaluated only once, its result is saved in a temporary variable
}

logical

OperatorMeaning
!exprNegation
expr || exprOr
expr && exprAnd
expr ^^ exprXor
expr -> exprImply
!true // false
true || false // true
true && false // false
true ^^ false // true
true -> false // false
xyx || yx && yx ^^ yx -> y
falsefalsefalsefalsefalsetrue
truefalsetruefalsetruefalse
falsetruetruefalsetruetrue
truetruetruetruefalsetrue

bitwise & shift

OperatorMeaning
~exprNot
expr | exprOr
expr & exprAnd
expr ^ exprXor
expr << exprShift left
expr >> exprShift right
0x22 & 0x0f // 0x02
0x22 & ~0x0f // 0x20
0x22 | 0x0f // 0x2f
0x22 ^ 0x0f // 0x2d
0x22 << 4 // 0x220
0x22 >> 4 // 0x02

conditional

OperatorMeaning
?exprExistential
expr?Existential
expr ?? exprNull coalescing
expr ? expr : exprTernary condition
if ?value {
// `value` is not null
}
if value? {
// `value` is not null
}
t = x ?? y ?? z // `t` is equal to the first not-null variable from `x`, `y` or `z` (in that order)
x = y == 42 ? 0 : 1 // `x` is equal to 0 is `y` equals `42`, if not `1`

The operator ?? is a polyadic operator.

typing

OperatorMeaning
expr is classType matching
expr is not classType not matching
expr as classType casting
expr:classNameType casting
class Foobar {
}
if x is Foobar {
// `x`'s type is `Foobar`
}
if x is not Foobar {
// `x`'s type isn't `Foobar`
}
t = (x as String).toUpperCase()
t = x:String.toUpperCase()
t = x as Array<String>

assignment

OperatorMeaning
expr = exprAssignement
expr -= exprSubtraction
expr += exprAddition
expr *= exprMultiplication
expr /= exprDivision
expr /.= exprQuotient division
expr %= exprModulo
expr &= exprBitwise and
expr |= exprBitwise or
expr ^= exprBitwise xor
expr <<= exprShift left
expr >>= exprShift right
expr ?= exprNon existential
expr ??= exprNull coalescing