Language
The language
Examplea whole program
shape Todo id: string title: string done: boolstate todos: Todo[] = [] persiststate draft = ""derive left = todos.filter(t => !t.done).lengthaction add if draft.trim() == "" return todos.push({id: uid(), title: draft, done: false}) draft = ""action remove(id: string) todos = todos.filter(t => t.id != id)column gap=8 padding=16 row gap=8 input value=draft placeholder="What next?" submit=add button "Add" press=add for t in todos key=t.id row gap=8 align-items=center input type=checkbox checked=t.done text t.title color=(t.done ? "dim" : "fg") button "×" press=remove(t.id) text `${left} left` color=dimMental model#
Declarations#
state#
Examplea whole program
state n = 0state name: string? = nonestate tags: string[] = []state theme = "light" persisttext `${n} ${name ?? "nobody"} ${tags.length} ${theme}`Addresses#
Examplea whole program
state address = "https://example.com:8443/find?q=oak+door&page=2#top"fn told(a: string): string let u = url.parse(a)? return `${u.host} port ${u.port}, looking for "${u.query.q}"`fn next(a: string): string let u = url.parse(a) else none if u == none return a return url.build({...u, query: {...u.query, page: Number(u.query.page ?? 1) + 1}})column gap=8 padding=16 input value=address text (told(address) else "not an address") text next(address) text `/tag/${url.encode("c++ & more")}`derive#
Examplea whole program
server fn quick(): number requires anyone return 1server fn slow(): number requires anyone return 2state got: json = {rows: ["a", "b"]}state n = 0derive rows: json[] = got.rows ?? []derive first: string? = rows.at(0)component Pill(label: string, loud: bool?) text label font-weight=(loud == true ? 700 : 400)action pick(fast: bool) n = fast ? quick()? : slow()?column gap=8 Pill loud label=`${first ?? "none"} of ${rows.length}` button "Pick" press=pick(true) text `${n}`action#
fn#
wasm fn#
Examplea whole program
shape Pt x: number y: numberfn far(p: Pt): number = Math.sqrt(p.x * p.x + p.y * p.y)wasm fn farthest(ps: Pt[]): number let most = 0 for p in ps most = Math.max(most, far(p)) return mosttext `${farthest([{x: 3, y: 4}, {x: 6, y: 8}])}`Examplea whole program
fn greet(name: string, title: string?): string = title == none ? `Hello, ${name}` : `Hello, ${title} ${name}`server fn visits(days: number, app: string?): number requires anyone return days * (app == "planner" ? 2 : 1)text greet("Ada")text greet("Ada", "Dr")shape#
Examplea whole program
shape Point x: number y: numbershape Line from: Point to: Point label: string?state l: Line = {from: {x: 0, y: 0}, to: {x: 3, y: 4}}fn length(l: Line) = Math.sqrt((l.to.x - l.from.x) * (l.to.x - l.from.x) + (l.to.y - l.from.y) * (l.to.y - l.from.y))text `${length(l)} ${l.label ?? ""}`A shape as a JSON Schema#
Examplea whole program
// A point on the page.shape Spot // Across, in pixels. x: number y: number label: string?server fn spotSchema(): string requires anyone = JSON.stringify(schema(Spot))text "schema"app#
Types#
none and T?#
Examplea whole program
shape User name: string age: numberstate users: User[] = [{name: "Ada", age: 36}]state picked: User? = nonefn label(u: User?): string if u == none return "nobody" return `${u.name}, ${u.age}`action pick(name: string) picked = users.find(u => u.name == name)column text label(picked) text (picked?.name ?? "no one yet") if picked text `${picked.age + 1} next year` button "Ada" press=pick("Ada")What is refused#
Logic#
Examplea whole program
state xs = [3, 1, 2]state name = ""state n = 0fn size(n: number): string match n 0 return "none" 1, 2 return "few" else return "many"action work let first = xs.at(0) if first == none return xs.push(first + 1) for x in xs if x == 1 continue if x > 100 break n += x let i = 0 while i < 3 i += 1 if n > 10 name = "big" else name = "small" let copy = [...xs, 9] let rec = {n, name, ...{extra: true}} log(copy, rec, i > 1 ? "a" : "b")text `${size(xs.length)} ${name}`button "work" press=workmatch#
Writes#
Built in#
time#
Examplea whole program
state zone = "Asia/Tokyo"state at = now()fn tomorrowAtNine(from: number, z: string): number let p = time.parts(from, z) return time.of({...p, day: p.day + 1, hour: 9, minute: 0, second: 0}, z)column gap=4 text `${time.clock(at, {zone, hours: 24})} there, ${time.clock(at)} here (${time.zone()})` text time.date(at, {zone, lang: "de", style: "full"}) text `the reminder is ${time.words(tomorrowAtNine(at, zone), {from: at})}` select value=zone for z in time.zones() option z value=zA call that can fail#
Examplea whole program
shape Post id: number title: stringstate report = ""fn count(url: string): number let xs: Post[] = net.get(url)? return xs.lengthaction tally let a = count("posts.json") else -1 let b = count("nope.json") else err => err.length let c = count("more.json")? report = `${a} ${b} ${c}`column button "Tally" press=tally disabled=pending(tally) text report if failed(tally) text `It failed: ${failure(tally) ?? ""}` color=dangerExamplea whole program
state when = ""state booked = 0action schedule if when.trim() == "" fail("Say when.")? booked += 1column gap=8 padding=16 input value=when placeholder="When?" button "Schedule" press=schedule if failed(schedule) text (failure(schedule) ?? "") color=danger text `${booked} scheduled` color=dimbytes#
Examplea whole program
state b: bytes = bytes("hi")state shot: bytes? = noneaction go let h = bytes.hex("6869")? let g = bytes.base64("aGk=") else bytes("") log(b.text(), b.hex(), b.base64(), b.length, b[0], b.slice(0, 1), h, g)if shot image shot.url()link "Get it" href=b.url("text/csv")button "go" press=goExamplea whole program
state said = ""fn packed(parts: bytes[]): bytes let head = bytes.new(8) head.set(bytes("MOTE"), 0) head.write(4, "u16", 1) head.write(6, "u16be", parts.length) let all = [head] for p in parts let n = bytes.new(4) n.write(0, "u32", p.length) all.push(n) all.push(p) return bytes.join(all)action build let file = packed([bytes("one"), bytes.of([1, 2, 3])]) let b = file.slice(0, 8) b[0] = 104 b.fill(0, 4) said = `${file.length} bytes, version ${file.read(4, "u16")}, ${file.read(6, "u16be")} parts, ${b.hex()}`button "Build" press=buildtext saidThe legacy multi-byte sets#
Examplea whole program
state said = ""fn greeting(b: bytes, label: string): string = b.text(label) else "?"server fn greetingThere(b: bytes, label: string): string requires anyone return greeting(b, label)action read let sjis = bytes.of([130, 177, 130, 241, 130, 201, 130, 191, 130, 205]) let big5 = bytes.of([167, 65, 166, 110, 129, 128]) said = `${greeting(sjis, "shift_jis")} ${greeting(big5, "big5")} / ${greetingThere(big5, "big5")?}`button "Read" press=readtext saidFiles and use#
Packages#
What a package wants of its taker#
Examplea whole program
// (In a package: what it wants of the program that takes it.)wants fn shopMay(): boolwants fn shopGreeting(name: string): string = `Welcome, ${name}`// (In the program: its own `shopMay`; `shopGreeting` is left to the package's default.)fn shopMay(): bool = truestate said = ""action hello said = shopMay() ? shopGreeting("Ada") : ""button "Hello" press=hellotext saidRefused, with the fix in the message#
Pitfalls#
In the source#
Edit this page:
docs/language.md