Docs / Syntax

Syntax

Kex borrows Ruby’s do … end blocks and identifier conventions, layers on type annotations with :, and uses -> for return types and = for expression-bodied functions.

Comments

# this is a line comment

Bindings

let binds an immutable name. var opts into local mutation. The ! suffix rebinds a var to a method’s updated return value.

bindings.kex
let name = "kex"          # immutable
var count = 0             # mutable
count = count + 1         # reassign

var list = [1, 2, 3]
list.push!(4)             # list := list.push(4)

Functions

A function is either a single expression after =, or a do … end block ending in return.

functions.kex
let double(n: Int) = n * 2

let greet(name: String) -> String do
  return "Hello, ${name}!"
end

let add(a, b) = a + b   # types can be inferred

Blocks and lambdas

blocks.kex
[1, 2, 3].map { |x| x * 2 }

[1, 2, 3].each do |x|
  IO.printLine(x.to(String))
end

Records and construction

point.kex
record Point do
  x : Float
  y : Float
end

let p = Point { x: 1.0, y: 2.0 }

Control flow

Branching is if / elif / else, optionally with a trailing if guard. Rich dispatch lives in match.

control.kex
return Error(EmptyInput) if s.empty?

if n > 0 then
  "positive"
elif n < 0 then
  "negative"
else
  "zero"
end

String interpolation

interp.kex
let name = "world"
"Hello, ${name}!"
"${(1..3).sum}"   # "6"