Functions

Meta: emphasize the four "feels weird at first" points: zero-arity has no parens; the body has no return; positional args can mix with named; &fn is for references. Each one separately surprises people.

Declaration

pub add(a: Int!, b: Int!): Int! { a + b }

Zero-arity

pub motd: String! { "hello" }

Auto-calling

Arguments

Named

greet(name: "Alice")

Positional

greet("Alice")

Mixed

Defaults

A non-null parameter with a default (name: String! = "world") is nullable on the caller's side but non-null on the receiver's side. Callers may omit it, pass null, or pass a nullable String; every such case falls back to the default. Inside the body the parameter is a plain String!, so no null checks or assertions are needed. This lets an API excise null at the boundary — prefer a non-null-with-default parameter over a nullable one whenever a sensible default (including a sentinel like "") exists, keeping both the caller (who can omit the argument) and the body (which never sees null) happy.

pub greet(name: String! = "world"): String! { "hi " + name }
greet                      # "hi world"  (omitted)
greet(null)                # "hi world"  (explicit null falls back)
greet(someNullableString)  # falls back to "world" when the value is null

Function references: &fn

Nested functions

Meta: link forward to Blocks — block arguments are the more common form of "pass code." Function refs are for the cases where you need a true callable to store or rebind.

Docstrings on parameters

pub greet(
  """name of the person to greet"""
  name: String!
): String! { ... }