$ cat learning-effect-2-building-tiny-effects.md
Part 2: Building Tiny Effects
$ tail -f learning-effect-2-building-tiny-effects/readers
connecting…
The first Effect post gave me the shape:
Effect.Effect<Success, Error, Requirements>;Now I want the smallest thing I can build with it.
No services. No architecture. No big theory.
Just success and failure.
A successful Effect
import { Effect } from "effect";
const answer = Effect.succeed(42);The type is:
Effect.Effect<number, never, never>;Read it slowly:
- success:
number - error:
never - requirements:
never
This Effect gives you a number. It cannot fail. It needs nothing.
A failed Effect
class MissingName { readonly _tag = "MissingName";}
const failed = Effect.fail(new MissingName());The type is:
Effect.Effect<never, MissingName, never>;That looks weird at first.
But it makes sense. This Effect does not produce a success value. It only fails.
So the success slot is never.
The pipe is just the path
Effect code often uses .pipe(...).
const program = Effect.succeed("trillo").pipe( Effect.map((name) => name.toUpperCase()),);This reads as:
- start with
"trillo" - turn it into uppercase
- return a new Effect
The program still has not run.
It is still a description.
What I am taking with me
succeed and fail are not magic.
They are just the two smallest Effect values:
Effect.succeed(value);Effect.fail(error);One fills the success slot.
One fills the error slot.
NOTE
If this feels too small, good. I want the boring pieces to feel boring before the bigger pieces show up