Records, choices and match
Most of what a Flow program handles is data with a known shape: a receipt, a manager's answer, the outcome of a refund. Flow has two ways to declare a shape. A record holds named fields, all of them at once. A choice is exactly one of several cases, and each case can carry data of its own. match takes a choice apart, and the checker makes sure no case is forgotten. The exact rules are in the Reference, under types and match.
Every example on this page is one file. Save it as shapes.flow to try them:
type Receipt { refund_id: Text, note: Option<Text> }type Answer { Approve, Deny }type Outcome { Refunded(Receipt), Declined { reason: Text, retry: Bool }, NotSent(Text),}flow describe(outcome: Outcome) -> Text = match outcome { Refunded(receipt) => "refunded as ${receipt.refund_id}", Declined { reason, retry: true } => "declined for now: ${reason}", Declined { reason, .. } => "declined: ${reason}", NotSent(_) => "not sent",}pub flow main(answer: Answer, amount: Int) -> List<Outcome> = { let receipt = Receipt { refund_id: "re_1042", note: None } let first = match answer { Approve => Refunded(receipt), Deny => Declined { reason: "the manager said no", retry: false }, } let second = if amount > 100 { NotSent("too large for the desk") } else { Refunded(Receipt { note: Some("partial"), ..receipt }) } [first, second]}Records
Receipt is a record with two fields. You build one by naming every field, Receipt { refund_id: "re_1042", note: None }, and read a field with a dot, receipt.refund_id. Records can't be changed; Receipt { note: Some("partial"), ..receipt } builds a new one with note replaced and every other field copied from receipt.
Choices
Answer and Outcome are choices. Answer's cases carry nothing, so a value is just Approve or Deny. Outcome's cases carry data in two styles: Refunded(Receipt) and NotSent(Text) carry one value each, written in parentheses, and Declined { reason, retry } carries named fields, like a small record. The ToolProblem every Tool call can return is a choice built the same way, with NetworkError { sent, reason } among its cases (Result, ToolProblem and ?).
Where the type is known from the context, a case is written bare, as in Refunded(receipt) above. Elsewhere it is named with its type, Answer.Approve, and from another module with the module as well: manager.Answer.Approve. When two types in reach share a case name, such as your own Declined and the payments library's, write the type to say which one you mean, as main.flow does with Outcome.Declined in A payment that timed out.
Taking a choice apart
match tries its arms from the top and takes the first whose pattern fits. A pattern can name the case and bind its data to names (Refunded(receipt)), require a field to have a value (retry: true), skip the rest of the fields (..) or ignore the data altogether (_). An arm can also take two patterns at once, A(x) | B(x), when both bind the same names.
The checker refuses a match that leaves a case out. Delete the NotSent(_) arm from describe:
flow check shapes.flowFound 1 error error[non-exhaustive-match] × this match does not cover `NotSent(_)` ╭─[shapes.flow:11:43] 11 │ ╭─▶ flow describe(outcome: Outcome) -> Text = match outcome { 12 │ │ Refunded(receipt) => "refunded as ${receipt.refund_id}", 13 │ │ Declined { reason, retry: true } => "declined for now: ${reason}", 14 │ │ Declined { reason, .. } => "declined: ${reason}", 15 │ ╰─▶ } ╰──── Why The subject has type Outcome; `NotSent(_)` is a value shape not covered by an unguarded arm. Help Add an arm matching `NotSent(_)` with the intended result, or add a final catch-all arm. Guarded arms do not establish exhaustiveness.A final arm of _, or a bare name such as other, matches anything left and satisfies the checker. Use it when "everything else" really is one decision. When each case deserves its own, list them: then a case added to the type later is an error at every match that hasn't decided it yet, and the checker finds each one for you.
The same values in a run's input
A run's input is written the way Flow writes the values themselves: a choice is its case's name, Deny, and a record is its type's name with every field, Receipt { refund_id: "re_1042", note: None }. Each KEY=VALUE gives one parameter, and one that isn't Text is written in that notation, so answer=Deny gives the choice and amount=40 the number:
flow run shapes.flow answer=Deny amount=40run b071c7a7029f[Declined { reason: "the manager said no", retry: false }, Refunded(Receipt { refund_id: "re_1042", note: Some("partial") })]b071c7a7029f completednext: flow replay b071c7a7029fThe result on standard output is written the same way, for people to read. The value notation in the Reference has the rules for each type.
The same values as JSON
An answer from outside a call, such as the manager's in Waiting for the manager, is written as JSON, where a choice is its name in quotes, as in '"Approve"', and so is the document --json prints. So every value also has a JSON form:
| Flow value | JSON |
|---|---|
a record, Receipt { refund_id: "re_1042", note: None } |
an object, {"refund_id":"re_1042"}: a None field is left out, and Some("partial") is written as "partial" |
a case with no data, Approve |
a string, "Approve" |
a case with one value, NotSent("too large") |
an object with one key, {"NotSent":"too large"} |
a case with named fields, Declined { reason, retry } |
an object with one key holding an object, {"Declined":{"reason":"…","retry":false}} |
Option outside a record field |
"None" or {"Some": value} |
Text, Int, Bool, List |
a string, a number, true or false, an array |
Unit, the value () |
[] |
The JSON form in the Reference covers every type.
Visibility
Everything in a file is private to it unless marked pub. Mark a type, a function or a field pub when another file uses it. A record marked pub shows only the fields that are pub themselves, so pub type Row { pub id: Text, note: Text } lets other files read id but not note. A pub choice shows all its cases. Everything a Tool library's contract declares is pub, which is why payments.Receipt and its refund_id can be used from main.flow.