[swift-evolution] Dotless case names in switch cases

Joe Groff jgroff at apple.com
Sat Jan 16 13:20:32 CST 2016


> On Jan 15, 2016, at 11:56 PM, Nate Cook <natecook at gmail.com> wrote:
> 
> 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

Definitely, Rob Rix has run into this problem in the wild too. I think that's a general ergonomic problem with our pattern syntax, though; 'let' on every pattern variable is verbose, and it's tempting when doing heavy pattern matching to reflexively type 'case let' at the start of every match to avoid it.

-Joe

>> On Jan 15, 2016, at 6:42 PM, Joe Groff via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> 
>>> On Jan 15, 2016, at 2:44 PM, Jens Persson via swift-evolution <swift-evolution at swift.org <mailto: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 <mailto: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/4f5e6e12/attachment.html>


More information about the swift-evolution mailing list