Effect.ts: Absence as First-Class
Advanced Patterns • Lesson 44 of 51
44. When to Use What - Absence Type Guide
Different types for different kinds of absence
Code Example
// Option: "value might be absent"
const optional: Option<User> = findUser(id);
// Either: "value absent, here's why"
const validation: Either<User, ValidationError> = validate(data);
// Effect: "value absent, describe HOW to get it"
const async: Effect<User, DbError, Database> = fetchUser(id);
// Ref: "value's CURRENT STATE is temporarily absent"
const state: Ref<number> = Ref.make(0);
// Queue: "values absent until produced"
const queue: Queue<Task> = Queue.bounded(100);
// Stream: "MANY values absent, arriving over time"
const stream: Stream<User> = Stream.fromIterable(users);
// Each models a DIFFERENT KIND of absence!
Interactive Example
const output: string[] = [];
output.push('Choosing absence types:');
output.push('');
output.push('Option: Simple maybe');
output.push('Either: Maybe with error context');
output.push('Effect: Async operation');
output.push('Ref: Mutable state');
output.push('Queue: Producer/consumer');
output.push('Stream: Many values over time');
output.push('');
output.push('Each models different absence!');
output.push('');
output.push(' Pick the right tool!');
return output.join('
');
Explanation
Absence type guide: choosing the right absence model.
All these types model absence, but differently:
- Option: simple presence/absence
- Either: absence with context
- Effect: absence requiring execution
- Ref: absence during state updates
- Queue: producer/consumer absence
- Stream: sequential absence over time
No single "absence" type! Choose based on:
- Is it a value or an operation?
- Does absence have context?
- Is it one value or many?
- Is it static or changing?
Absence-first design: pick the right absence model!
Part 44 of 51 in the Effect.ts Absence Modeling series