Operators
Operators in kex are just methods with symbolic names, attached to a type
through make. The same symbol can resolve to different
implementations depending on the receiver.
Operator table
| Operator | Purpose |
|---|---|
| + | addition / concatenation |
| - | subtraction |
| * | multiplication |
| / | division |
| % | modulo |
| == != | equality |
| < > <= >= | comparison |
| && || | boolean logic |
| & | function / method reference |
| ~ | partial application |
| ? | early-return propagation |
| .. | range (1..10) |
| @ | @field shorthand in make |
Overloading
Define the operator as a method inside make. Dispatch is by
receiver type, so Vector2D.+ and Vector3D.+ can
coexist.
overload.kex
make Vector2D do
let +(other: Vector2D) -> Vector2D do
return Vector2D { x: @x + other.x, y: @y + other.y }
end
let *(factor: Float) -> Vector2D do
return Vector2D { x: @x * factor, y: @y * factor }
end
end
# Operator sugar resolves through the receiver type:
# Vector2D.UnitX * 2.0 + Vector2D { x: 0.0, y: 5.0 } Conversion convention
A method named to taking a type is the canonical conversion.
value.to(String) is how values render.