Introducing an “Unwrap or Throw” operator

Am I correct in thinking that there are multiple different desires in play?

  1. "unwrap or throw" as just sugar to transform an optional unwrap to a throwing error.
  2. "unwrap or die" alternatives to force unwrapping (and crashing if nil) because we want to be explicit about the reasons.

I see these things as separate goals. The linked package seems to focus on number 2 because it fatalError on DEBUG.

I personally always want 1, never 2. I want sugar to throw an error if nil, but that's something I expect to happen, is not an edge case that needs to fatal error.

IMO ideally we should have both behaviours using the same syntax and being explicit.

try optional ?? throw(Error())
optional ?? fatalError("fancy message")
1 Like