Swift: "case" should be set free!

Originator:garth
Number:rdar://21540814 Date Originated:24-Jun-2015 11:34 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Swift 2
Classification:Enhancement Reproducible:Always
 
It’s nice that you can now use elaborations such as “if case” in Swift 2, but why are these so closely tied to specific statements? 

It seems from my outsider’s perspective that case has two simultaneous functions: to determine whether a pattern matches a given value, and to bind variables and constants for an ensuing scope. But often, it would be helpful to access the first of these functions without the second.

A case in point: a protocol engine has a state machine defined as an enum. In the Connected state, the associated value identifies the peer. Currently, to implement a simple isConnected property, I believe you have to write something like:

    var isConnected: Bool {
        if case .Connected(_) = state {
            return true
        } else {
            return false
        }
    }

But really, you just want to know whether the state matches the .Connected(_) template; why can’t you just write:

    var isConnected: Bool {
        return case .Connected(_) = state
    }

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!