Daily Thought - 2024-08-14
< back to listSo what were the problems with if
that led me to
pattern matching? There are two: Call stack reconstruction
and linear types. Let's start with linear types, because to get to call stack
reconstruction, we need more background.
Okay, so a value of a linear type can only be used once, meaning if we want to use it in a closure, the closure needs to take ownership. That means we can't have two closures using the same value. (If the value is of a linear type. Not all types have to be linear.)
So if we model if
as a built-in function with two closure parameters, then
both can't take ownership of the same linear value. It would be fine in
principle, because only one of them gets executed, but the compiler doesn't know
that. To the compiler, a built-in function is opaque.