Skip to content
Noodle
InstallLearnPlayground
GitHub

Datatypes and basic patterns

A datatype introduces a nominal type with a closed set of constructors. Use it when a value may have one of several meaningful shapes.

Constructors may have no payload, positional payloads, or named payloads:

datatype Message
Quit
Text(String)
Move{x: Int, y: Int}
end

There is no punctuation between constructors. Construct values with an explicit Type::Constructor path:

datatype Message
Quit
Text(String)
Move{x: Int, y: Int}
end
func initial_message() -> Message do
Message::Move{x: 0, y: 0}
end

Constructor names need to be unique only inside their own datatype.

A switch evaluates its target once and considers cases from top to bottom:

datatype Message
Quit
Text(String)
Move{x: Int, y: Int}
end
func describe(message : Message) -> String do
switch message
case Message::Quit then "quit"
case Message::Text(text) then text
case Message::Move{x, y} then
"move " ++ x.to_string() ++ "," ++ y.to_string()
end
end

The checker requires the cases to be exhaustive. Every case body must produce one common result type. Bindings such as text, x, and y exist only in their own case body.

_ matches any value without creating a binding. A lower name matches any value and binds it:

datatype Status
Ready
Waiting(Int)
Failed(String)
end
func is_ready(status : Status) -> Bool do
switch status
case Status::Ready then true
case _ then false
end
end

Use a wildcard only when all ignored cases are intentionally equivalent. A fully enumerated switch is more robust when different alternatives should remain visible.

A single-constructor datatype is useful when a value needs nominal identity:

datatype UserId
UserId(Int)
end
func raw_id(id : UserId) -> Int do
id._0
end

Direct payload field access is available because the static type determines the only possible constructor. Named payloads use their declared field names. For a multi-constructor datatype, inspect the value with switch instead.

Generic datatypes, guards, nested patterns, aliases, and or-patterns are covered in Advanced pattern matching and Generic programming.

Next: error handling.