Docs / Operators

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
== != equality
< > <= >= comparison
&& || boolean logic
&. receiver shorthand (&.method)
~ capture / partial application (~f, ~(+))
... spread into a list or map
.. range (1..10)
! mutating call on a var (list.push!(4))
@ @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:
# unitX * 2.0 + offset
examples/overload.kex

Conversion convention

A method named to taking a type is the canonical conversion. value.to(String) is how values render.