A program is a valueLesson 5 of 23

Where does it get what it needs?

Ask for a dependency instead of reaching for it, and swap what answers.

welcome greets Ada. It doesn't know how to greet anybody, in any language. It asks for something called a Greeter and uses whatever it's given.

Predict

Look at the last line: welcome.pipe(Effect.provide(Greeter.english)). Suppose you deleted the pipe and exported welcome by itself. What would happen when it ran?

Check your answer

It would stop at yield* Greeter with an error saying the service app/Greeter was not found. welcome asks for a Greeter, and nothing answered.

In your own editor you wouldn't get that far. The comment gives welcome's type, Effect<string, never, Greeter>, and the third slot says what it still needs. Running an Effect requires that slot to be empty, so the missing provide is a type error. The sandbox strips types, so here you can watch it fail: try it, then put the line back.

Try it

  1. Read the body of welcome. Which line decides that the greeting is in English?

  2. Give Greeter a second way to be built, next to english:

    static readonly french = Layer.succeed(
      Greeter,
      Greeter.of({ greet: (name) => Effect.succeed(`Bonjour, ${name} !`) })
    )

    Does anything in welcome have to change?

  3. Provide Greeter.french instead of Greeter.english. Before you run: what prints?

  4. Run the same welcome both ways in one program:

    export const program = Effect.gen(function*() {
      const english = yield* welcome.pipe(Effect.provide(Greeter.english))
      const french = yield* welcome.pipe(Effect.provide(Greeter.french))
      return [english, french]
    })

    How many times did you edit welcome in this lesson?

About step 1

None of them. The language is decided on the last line of the file, outside welcome, by whoever runs it.

What you found

Greeter is a service: a name, and the shape of what it can do. yield* Greeter asks for it. A Layer is one way of building it, and Effect.provide hands a Layer to a program.

The type now has all three of its slots: Effect<A, E, R>. What it produces, how it can fail, and what it needs. The third behaves like the second. A need nobody has met stays in the type, and one that has been provided leaves it.

This is the same move as the last two lessons, pointed at dependencies. The program states what it needs, and whoever holds it decides what answers: the real warehouse in production, a fake in a test, French on Tuesdays. The program can't tell the difference and doesn't have to.

Whoever holds a program decides how often it runs and what it talks to. There is one more thing they might want to decide. Once it's running, who can stop it?

05
Principle 5

Ask for what you need

A program names its dependencies and receives them from outside, so the same program runs against the real thing, a fake or a test double.

The next questionWho can stop it? →
OutputNot run yet

Not run yet

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