A program is a valueLesson 4 of 23

How do you know it can fail?

Read a program's failures in its type, and handle one by name.

A shop has three boxes of tea and no coffee. order packs one of whatever you ask for, and program asks for coffee.

Predict

Read order, then run program in your head. How does the output end: with a thrown exception, with a line of text, or with something else?

Check your answer

With a failure, and the output names it: OutOfStock. Nothing was packed and nothing was thrown.

yield* new OutOfStock({ item }) ends the program at that line, the way Effect.fail did in the last lesson. The reason it gives is an ordinary value, with a name and a field, item, that says what was missing. Press Run to see it.

Try it

  1. The comment above order gives its type: Effect<string, OutOfStock>. The first slot is what it produces. What is the second? And who wrote it there?

  2. Below order, write a version that can't fail:

    const orderOrApologise = (item: string) =>
      order(item).pipe(
        Effect.catchTag("OutOfStock", (error) =>
          Effect.succeed(`Sorry, we have no ${error.item} today`))
      )

    Before you run anything: what is its type?

  3. Make program an Effect.gen that logs yield* orderOrApologise("tea") and then yield* orderOrApologise("coffee"). What prints?

  4. Sometimes you want both outcomes as plain data. Return this from program, and predict the value:

    yield* order("coffee").pipe(
      Effect.match({
        onSuccess: (text) => ({ _tag: "SucceededOrder", text }),
        onFailure: (error) => ({ _tag: "FailedOrder", item: error.item })
      })
    )
About steps 1 and 2

The second slot is how it can fail, and nobody wrote it. TypeScript read it off the yield* new OutOfStock line. Add a second kind of failure to order and the slot would list both.

orderOrApologise is Effect<string, never>. You handled OutOfStock by its name, so it left the type, and never means there is no failure left. Misspell the name as "OutOfStok" and your editor refuses, because order has no failure called that.

What you found

A function that throws tells its caller nothing. Its type says string, and the exception is a surprise you learn about in production. Here the failure is a value like any other, and it sits in the type next to the result: Effect<string, OutOfStock>.

That makes the type an honest summary. A failure you haven't dealt with stays visible in it, all the way up to whatever finally runs the program. One you have dealt with is gone from it. catchTag recovers from one failure by name, and match turns both outcomes into values. Like retry in the last lesson, both are applied from outside.

order has one more habit worth a look. It reads stock, a variable that happens to be lying in the same file. A real shop would have to ask a warehouse. Where should a program get the things it needs?

04
Principle 4

The type tells the truth

An Effect's type says what it produces and how it can fail. A failure you handle leaves the type. One you forgot is still there to see.

OutputNot run yet

Not run yet

Read the code and say what it will do. Then press Run.