[swift-evolution] Dotless case names in switch cases

Nate Cook natecook at gmail.com
Sat Jan 16 01:56:44 CST 2016


There's a fairly subtle difference between `case Foo` and `case let Foo` when using a switch inside an enumeration. The first matches the Foo case of the current enumeration, the second binds a local `Foo` (that shadows .Foo) to any case. See the `testLet` property in this example:

enum Test {
    case One, Two
    
    var testDot: String {
        switch self {
        case .One: return "One"
        case .Two: return "Two"
        }
    }
    
    var testLetDot: String {
        switch self {
        case let .One: return "One"
        case let .Two: return "Two"
        }
    }
    
    var testLet: String {
        switch self {
        case let One: return "One"  // Danger Will Robinson
        case let Two: return "Two"
        }
    }
    
    var testNone: String {
        switch self {
        case One: return "One"
        case Two: return "Two"
        }
    }
}

let two = Test.Two
two.testDot         // Two
two.testLetDot      // Two
two.testLet         // One
two.testNone        // Two


> On Jan 15, 2016, at 6:42 PM, Joe Groff via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> On Jan 15, 2016, at 2:44 PM, Jens Persson via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> enum SomeEnum {
>>    case Foo
>>    case Bar
>>    func demoDotAndNoDot() {
>>        switch self {
>>        case .Foo: print("This has the usual dot before the case name.")
>>        case Bar: print("This has no dot, which is ok, at least in this particular context.")
>>        }
>>    }
>> }
>> 
>> I'm just throwing this out in case someone should feel like writing a formal proposal to remove the dotless form.
>> 
>> I'd be ok with removing it since it only works in some specific contexts. 
> 
> This works by normal instance member lookup. Why do you think it should be removed?
> 
> -Joe
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160116/ecc90c42/attachment.html>


More information about the swift-evolution mailing list