The Flow Book
  1. Why Flow

One week at the support desk

  1. A payment that timed out
  2. What the run was told
  3. Writing the payments library
  4. Waiting for the manager
  5. The next morning
  6. When the process dies mid-call
  7. Fixing a bug in a half-finished run
  8. Three carriers at once
  9. Answering calls from your own service

Projects

  1. What people build
  2. A research agent you can check

The language tour

  1. Values, functions and blocks
  2. Records, choices and match
  3. Modules, libraries and Python functions
  4. Result, ToolProblem and ?

Appendix

  1. The flow command
Language reference ↗

Result, ToolProblem and ?

A failure your program handles is a value in Flow. There is no exception to throw or catch: a function that can fail says so in its result type, and the caller takes the result apart like any other value. This page covers the three pieces involved, Result, the ToolProblem every Tool call can return, and ?, with the support desk's refund as the running example. The exact rules are in the Reference, under Results, Tool problems, and faults.

Result

Result<T, E> is an ordinary enum with two cases, Ok(T) and Err(E), and it is always in scope. E can be any type, usually an enum you declare:

type RefundError { Unanswered(ToolProblem), Refused(payments.PayError) }

An Err doesn't leave the function that holds it, stop a loop or travel up the stack. It is a value until some code looks at it. To reach what's inside an Ok, you have to say what happens to the Err, with a match that covers both, or with ? below. To drop a result without reading it, write let _ = ...; the checker refuses a result left unused without that.

Option<T> is the type for a value that may be absent, such as a lookup that found nothing. It isn't a failure, and it has no ?; take it apart with match.

What a Tool call returns

A Tool's result type is whatever its function returns. The payments library's refund returns Result<Receipt, PayError>, the provider's own answer, and find_refund returns Option<Receipt>. Calling a Tool wraps that in one more Result:

Tool declared as The call's type
refund(...) -> Result<Receipt, PayError> Result<Result<Receipt, PayError>, ToolProblem>
find_refund(...) -> Option<Receipt> Result<Option<Receipt>, ToolProblem>
draft_reply(...) -> Text Result<Text, ToolProblem>

The outer Ok means the call got its answer, and what's inside is that answer. The outer Err holds a ToolProblem, one of three cases:

pub type ToolProblem {    NotStarted(Text),                          // the function never started    Interrupted(Text),                         // it started but did not return a value    NetworkError { sent: Bool, reason: Text }, // the network to a network Tool failed}

NotStarted and NetworkError { sent: false, .. } say that nothing was done, so calling again can't repeat anything. Interrupted and NetworkError { sent: true, .. } make no such promise. The Text in each case is the reason as the library wrote it; Flow doesn't read it. A payment that timed out is the chapter about choosing between them, and Writing the payments library is the other side, where a library decides which one to report.

A match on a Tool call usually has arms at both layers:

match payments.refund(order, dollars) {    Ok(Ok(receipt)) => Refunded(receipt),    Ok(Err(error)) => Outcome.Declined(error),    Err(NotStarted(reason)) | Err(NetworkError { sent: false, reason }) => NotSent(reason),    Err(Interrupted(reason)) | Err(NetworkError { sent: true, reason }) => Unsure(reason),}

A ToolProblem is data like any other value. You can keep it in a record, return it, or put it in text, where it prints as Flow source: "${problem}" gives NetworkError { sent: true, reason: "no answer from the provider in 10s" }.

?

? after a Result hands over the Ok value, or returns the Err from the enclosing function at once. It returns; it never retries. These two lines mean the same thing:

let answered = payments.refund(order, dollars)?let answered = match payments.refund(order, dollars) {    Ok(value) => value,    Err(problem) => return Err(problem),}

The enclosing function must return a Result whose error type is exactly the error's type: here Result<_, ToolProblem>. Flow converts nothing between error types on its own. To change the error on the way out, write x.map_err(f)?, which returns Err(f(e)) instead. A constructor is a function, so wrapping the error in one of your own cases takes one word:

flow refund(order: Text, dollars: Int) -> Result<payments.Receipt, RefundError> = {    let answered = payments.refund(order, dollars).map_err(Unanswered)?    let receipt = answered.map_err(Refused)?    Ok(receipt)}

Each ? takes off one layer. The first takes off the ToolProblem and leaves the provider's Result<Receipt, PayError>; the second takes off the provider's PayError. The two can share a line: payments.refund(order, dollars).map_err(Unanswered)?.map_err(Refused)?.

A function that returns its own outcome enum, rather than a Result, can't use ?. Put the steps that use ? in a helper that returns Result, and match on the helper's result once.

Answers from outside a call

A Tool's library gives every answer its calls get, including one that comes from somewhere else, such as a webhook or a person at a terminal. In Python, the toolkit's reply(key, value) answers the open call that key names, the key a Tool function reads as context().key, and takes the same four shapes the function itself can give:

Call The call's answer
reply(key, value) Ok(value): the Tool answered value
reply(key, NotStarted(reason)) NotStarted: the work never started
reply(key, Interrupted(reason)) Interrupted: the work started and stopped before a value
reply(key, NetworkError(sent=True, reason=reason)) NetworkError: the network failed; sent says the request had left

The value is checked against the call's type before anything is recorded. refund's result type is the provider's Result<Receipt, PayError>, so its value carries the provider's Ok or Err as well: Ok(Receipt("re_1042")), whose JSON form is {"Ok":{"refund_id":"re_1042"}}. Leave the provider's layer out and the value doesn't have the call's type: reply reports a type error and records nothing. The first answer recorded is the call's, and a later reply changes nothing: it reports that the call was already answered, and whether the answer it has equals this one. Answering calls from your own service says which runs reply can reach.

A library that is asked again instead, as the manager's is in Waiting for the manager, gives the same answers from the Tool function: it returns the value, or raises the problem.

A run's result prints the same way when it is a Result: hand-back.flow's run in A payment that timed out ended {"Err":{"NetworkError":{"reason":"no answer from the provider in 10s","sent":true}}}.

Faults

A few things stop a run instead of producing a value: dividing by zero, waiting on a task that was stopped, and running past a limit such as the size of a number. These are faults. A fault stops the run and leaves it open, with no end in its record, and the command exits with status 3 and says which fault it was. No code can catch one, and a program can't raise one: Flow has no panic, assert or unwrap, and an impossible state is an error case like any other. What you can do with a run after a fault is in Fixing a bug in a half-finished run.