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
Read the body of
welcome. Which line decides that the greeting is in English?Give
Greetera second way to be built, next toenglish:static readonly french = Layer.succeed( Greeter, Greeter.of({ greet: (name) => Effect.succeed(`Bonjour, ${name} !`) }) )Does anything in
welcomehave to change?Provide
Greeter.frenchinstead ofGreeter.english. Before you run: what prints?Run the same
welcomeboth 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
welcomein 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?
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.