Unwrap or Throw - Make the Safe Choice Easier

You could play with the idiom by putting a simple wrapper function around throw today:

// could be -> Never when Never-as-bottom is implemented
func raise<T>(_ error: Error) throws -> T {
  throw error
}

try value ?? raise(Errors.falseResult)

We originally decided against making throw an expression because it would be inconsistent with other control flow statements like break, continue, and return. With Never in the type system, and rethrows autoclosures, we could conceivably make it so that break, continue, and return were also expressions, and plumb them through autoclosures as if they were thrown errors so that they can branch out to the right places in the outer function. This would allow ?? to be used as shorthand for a lot of trivial one-line guard lets that unwrap an optional or immediately exit scope on nil.

17 Likes