Another reason why exceptions aren't zero-cost in practice is that compilers need to model the exceptional control-flow, and they usualy don't do a great job in this situation and miss a lot of potential optimizations.
I contest. The reason for the condition need not be only to indicate whether the other value isn't null; if it was, you could just remove the condition and check nullity. In fact, unless there is either a case where the condition is true and the pointer is null or a case where the condition is false and the pointer is non-null, the condition is useless because checking the pointer would've been enough.
Consider the following silly example, where we have a function
f(Conductor *c, bool isDriving, Train *t)
with the guarantees that c != NULL, and
isDriving==true => t!=NULL. Note that the case isDriving==false does not imply t==NULL; indeed, c could just be waiting for the signal to start driving. enum ConductorTask {
Nothing,
Waiting(Train),
Driving(Train)
}
fn f(c: Conductor, task: ConductorTask)
I think this says everything that your text says, but in a way the compiler understands and can assist the programmer with.Alternatively, since isDriving only makes sense when there's a train, t could be Option<(bool, Train)>, i.e. an optional train along with a boolean for if it's being driven or not.
If it can't, then the version without the check is potentially unsafe, and the check is not unnecessary.
They aren't zero-cost abstractions in most languages. Every unwrap() potentially results in a dynamic check, even if one isn't necessary. There are a number of ways you could extend a type system to track the potential cases of sum types, which would help alleviate this cost.