A program's own data is declared as structs (a value with named fields) and enums (a value that is one of several variants). Both are nominal: a type is identified by its name and module, not by its fields. Both are closed: every field and every variant is declared, and nothing else can be added at run time.
Structs
struct Point { x: i32, y: i32 }.That declaration belongs at the top level of a module. Fields are written
name: Type, separated by commas; a trailing comma is allowed, and a struct
may have no fields at all (struct Marker {}.). A field's type may be any
type, including a list of the struct itself:
module Example::Trees.
export struct Node { label: Text, children: List[Node] }.
export leaf label: Text -> Node [
ret Node { label: label, children: List empty: Node }.
].Building a record
A record is built by naming the struct and giving every field exactly once, in any order:
module Example::Points.
export struct Point { x: i32, y: i32 }.
export origin -> Point [
ret Point { y: 0, x: 0 }.
].
export moved by: i32 -> Point [
p := Point { x: 1, y: 2 }.
ret Point { x: (p x) + by, y: (p y) }.
].
export labelled -> Point [
ret #( x: 3, y: 4 ).
].A missing, unknown or repeated field is CAP-TYPE-014. There are no
default values: a reader of Point { ... } sees every field's value.
module Example::Points.
struct Point { x: i32, y: i32 }.
export origin -> Point [
ret Point { x: 0 }.
].Where the expected type is already known, as in a ret from a procedure
declared -> Point, a labelled shape #( x: 3, y: 4 ) builds the same
record. Lists and shapes has the rules.
Reading fields
A field is read in parentheses, (p x). Fields of nested records chain:
(order customer name).
Records never change. There is no field assignment: to "change" a field,
build a new record with the new value, as moved by: does above.
$ captain build points.capt -o points.capb
built points.capb ([..] bytes, sha256 [..])
$ captain run points.capb origin
run log: origin.runlog
Point { x: 0, y: 0 }
$ captain run points.capb moved by=10
run log: moved.runlog
Point { x: 11, y: 2 }
$ captain run points.capb labelled
run log: labelled.runlog
Point { x: 3, y: 4 }Nominal typing
Two structs with the same fields are still different types:
module Example::Nominal.
struct Point { x: i32, y: i32 }.
struct Size { x: i32, y: i32 }.
export area -> Size [
ret Point { x: 2, y: 3 }.
].A Point is not a Size, however alike they look. When a host passes a
record into an entry point, it is checked against the declared type
exactly: nominal and closed, with nothing coerced
(Values in and out).
Structs have no =. Comparing two records is CAP-TYPE-011; compare the
fields that matter: ((a x) = (b x)) & ((a y) = (b y)).
Enums
An enum's value is one of its variants. A variant either carries named fields (a payload) or carries nothing:
module Example::Enums.
export enum Outcome [
Shipped(tracking: Text).
Refunded(amount: Money).
Cancelled.
].
export ship code: Text -> Outcome [
ret Shipped { tracking: code }.
].
export cancel -> Outcome [
ret Cancelled.
].Each variant ends with a ., and an enum has at least one. A payload
variant is built like a record, with every field; a variant without a
payload is written by its name alone.
$ captain build enums.capt -o enums.capb
built enums.capb ([..] bytes, sha256 [..])
$ captain run enums.capb ship code=ZX-1
run log: ship.runlog
Shipped { tracking: "ZX-1" }
$ captain run enums.capb cancel
run log: cancel.runlog
CancelledVariant names share one namespace across a module: two enums in the same
module cannot both have a Cancelled (CAP-RESOLVE-004). A variant may also
be written with its enum's name, Outcome::Cancelled.
Taking an enum apart
Option and Result, the two enums the language declares, have
combinators that run a block for the variant a value holds:
ifSome:ifNone:, ifOk:ifFail: and the others in
Option and Result.
A program's own enums have no such combinators yet, no match, and no =.
Today a program can build, pass, store and return a value of its own enum,
and a host can read it, but Captain code cannot branch on which variant it
holds. A general match is deliberately deferred (spec 03 §9). When code
must choose between a few named cases, use a symbol, which compares with
=, or a bool.
Generic enums
An enum, like a struct, may take type parameters. A variant's type arguments come from the context it is used in:
module Example::Either.
export enum Either[L, R] [ Left(v: L). Right(v: R). ].
export left n: i32 -> Either[i32, Text] [
ret Left { v: n }.
].A variant with nothing to say what its type arguments are is refused, as
for None on its own (CAP-TYPE-018).
Types as arguments, and generics covers generic
types.
Aliases
type Name := Text. declares an alias: another spelling of the same type.
An alias is transparent: a Name is a Text and a Text is a Name.
module Example::Aliases.
type CustomerName := Text.
export greet name: CustomerName -> Text [
ret "Dear " append: name.
].An alias documents intent, but the checker does not keep the two apart. When
it should, declare a struct with one field: struct CustomerId { value: Text }.
is a new type that no Text can be passed as.
Types from another module
An exported struct or enum is named through the module that declares it,
qualified like its procedures: Orders::Order, List[Orders::Order].
module Shop::Orders.
export struct Order { item: Text, quantity: i32 }.
export enum Status [ Open. Closed(reason: Text). ].
export order item: Text quantity: i32 -> Order [
ret Order { item: item, quantity: quantity }.
].
export close reason: Text -> Status [
ret Closed { reason: reason }.
].Another module can take, return and read those types, and pass their values on:
module Shop::Front.
use Shop::Orders.
export single item: Text -> Orders::Order [
ret Orders::order item: item quantity: 1.
].
export quantityOf o: Orders::Order -> i32 [
ret (o quantity).
].
export withdraw why: Text -> Orders::Status [
ret Orders::close reason: why.
].What another module cannot do is build them directly. A record literal
takes an unqualified type name, so Orders::Order { ... } does not parse,
and the variants of an imported enum cannot be named:
module Shop::Front.
use Shop::Orders.
export reopen -> Orders::Status [
ret Orders::Open.
].So a module whose types other modules need to build exports procedures
that build them, as Orders::order item:quantity: and
Orders::close reason: do. The standard library does the same:
Http::header name:value: builds an Http::Header. Qualified record
construction is planned (DR-0014) but not implemented.
$ mkdir -p shop && cp orders.capt shop/
$ captain build front.capt -I shop -o front.capb
built front.capb ([..] bytes, sha256 [..])
$ captain run front.capb single item=lamp
run log: single.runlog
Shop::Orders::Order { item: "lamp", quantity: 1 }
$ captain run front.capb withdraw why=duplicate
run log: withdraw.runlog
Shop::Orders::Status::Closed { reason: "duplicate" }At run time a type's identity includes its module, which is why the result
prints as Shop::Orders::Order. Two modules may each declare an Order,
and the checker never lets one stand in for the other.