[Pitch] Optional.orThrow

Worth to note: there's a critical difference between x! and anything we've brought up so far: x! could be used as an L-value:

x! = value
x!.mutatingFunc()
x![1] = value
x!.field = value

whereas

try x ?? throw error = value

or a similar syntax would result in:

// :stop_sign: Cannot assign to value: binary operator returns immutable value

Would be cool if the "throwing optional unwrap" equivalent supported L-value usage scenario.


In light of the L-value issue, here's another suggestion: introduce a special built-in postfix ^ "operator", that for all intents and purposes works like ! except it throws on nil instead of terminating the app:

try x^.mutatingFunction()
try x^ = value
try x^[1] = value
try x^.field = value
value = try x^

When the value is nil it'd throw some fixed non customisable "Nil" error.

That'd require a compiler change as it can't be done by current language means.

Similarly as^ could be a throwing version of as!