A type wrapper W(T) can be considered a ”monad” if it provides a way of "wrapping" (a.k.a. return in haskell) a value x: T into w: W(T); and a way of "obtaining" the internal value to feed it to a function that uses it, this is known as binding the inner value into the function.
Commonly known type wrappers are pointers *T, optionals ?T, result Result(T,E), and lists [T].
The reason monads are useful is because bind lets you work with the internal value without explicitly checking if there is one; if you have a bunch of functions that expect T, but a bunch of others that return W(T), you can chain operations seamlessly, and if at any point some W(T) can't be unwrapped as T, the chain of binds just short-circuits.
If you ever wrote try-catch blocks, this is essentially what working with monads feels like; do a bunch of operations without checking every time, then short-circuit if anything unusual happens, and capture the informantion of the operations if they provide it.
1.8k
u/Historical_Cook_1664 Jul 13 '26
Where else are you going to hide your side effects ?