The terminal object () has the property that, for any type T, there is a unique function of type fn(T) -> ().
What do I mean by “unique?” Well, we will declare functions to be the same if they return equal outputs for equal inputs. So when I say there is a unique function with that signature, I mean that all functions with that signature return equal outputs for equal inputs.[1]
Now suppose that:
fn f(x: i32) -> i32;
fn g(x: i32) -> ();
fn h(x: ()) -> &'static str;
The fact that () is a terminal object allows us to reason about these functions in various ways simply by looking at the types:
- We can argue that
|x| g(f(x)) is the same as |x| g(x), because both have type fn(i32) -> (), and there is a unique function of that type.
- This lets us further argue that
|x| h(g(x)) is the same as |x| h(g(f(x))), by substitution.
The initial object ! has similar properties, but in the opposite direction; for any T, there is a unique function of type fn(!) -> T. (effectively, every such function is just |x| match x { })
[1] Of course, this only works for pure, total functions; I am basing this category off of Hask.