[swift-evolution] [Discussion]: Deprecate !-Unwrapping of Optionals

Developer devteam.codafi at gmail.com
Mon Feb 29 15:26:03 CST 2016


I was thinking something more like

func unsafeUnwrap<T>(opt : Optional<T>) -> T {
    switch opt {
        case let .Some(val):
            return val
        default:
             fatalError("Unexpectedly found nil while unwrapping optional value")
    }
}

Bang doesn't go away, it just becomes more obvious that you're making a leap of logic (as it were).

~Robert Widmann

2016/02/29 15:59、davesweeris at mac.com のメッセージ:

> 
>> On Feb 29, 2016, at 2:24 PM, Developer via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> And for those cases there will be a standard library function or something more obvious than bang to make up for the deprecated operator.  Force unwrapping will not go away (that's a terribly unproductive idea), but the goal is to make a dangerous operation more obvious[ly dangerous].  
> 
> Like this?
> enum OptionalUnwrappingError : ErrorType {
>     case unexpectedlyFoundNil
> }
> enum Optional<T> {
>     typealias Wrapped = T
>     case None
>     case Some(Wrapped)
>     ...
>     func unwrap() throws -> Wrapped {
>         switch self {
>         case .None: throw OptionalError.unexpectedlyFoundNil
>         case .Some(let value): return value
>         }
>     }
> }
> 
> It’s hard to get more obvious than the compiler complaining that you haven’t wrapped a throwing function in a try block
> 
> - Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160229/31aebdbb/attachment.html>


More information about the swift-evolution mailing list