Skip to main content

Unit

What is Unit?

Unit is a type with exactly one value. It is ribs' equivalent of Dart's void, used wherever a type is required but the value carries no information — for example, a State<S, Unit> that only transitions state without producing a result, or an IO<Unit> that performs an effect and returns nothing meaningful.

Why not void?

Dart's void is not a proper type — it be avoided as a type argument. You cannot write List<void>, Option<void>, or IO<void> in a way that composes cleanly. void is a compiler hint that a value should be discarded, not a first-class type that should appear in generic positions.

Unit has no such downsides. Because it is a regular Dart class, it can appear anywhere a type argument is expected:

IO<Unit>              // an effect that produces no meaningful result
State<S, Unit> // a state transition with no return value
Either<String, Unit> // a computation that either fails or simply succeeds

There is only ever one instance of Unit in memory — Unit() always returns the same Unit.instance singleton — so using it as a placeholder type has no overhead.

tip

For further background on void in Dart and why a dedicated unit type is useful, see: The Curious Case of Void in Dart