<div dir="ltr">I would like to add a syntax sugar .casesBelow for enum value to be used in switch sentence to avoid default case.<div><br></div><div>enum MyEnum {</div><div> case a</div><div> case b</div><div> case c</div><div>}</div><div><br></div><div>let myEnum: MyEnum = .a</div><div><br></div><div>//Normally we need default case</div><div>switch myEnum {</div><div> case .a: print("a")</div><div> default: print("other value")</div><div>}</div><div><br></div><div>//Now use syntax sugar</div><div><div>switch myEnum.casesBelow {</div><div> case .a: print("a")</div><div>}<br></div></div><div><br><div><br></div><div>This would look more intuitive to me than other solutions but I am not sure how much effort we need for this.<div><br></div><div><br></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Jan 15, 2018 at 4:36 AM Vladimir.S via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
<br>
On 12.01.2018 21:38, Jordan Rose wrote:<br>
><br>
><br>
>> On Jan 12, 2018, at 06:49, Michel Fortin via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
>> <mailto:<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>> wrote:<br>
>><br>
>>> Le 12 janv. 2018 à 4:44, Vladimir.S via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
>>> <mailto:<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>> a écrit :<br>
>>><br>
>>> On 12.01.2018 10:30, Chris Lattner via swift-evolution wrote:<br>
>>>>> On Jan 11, 2018, at 11:15 PM, Jean-Daniel via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
>>>>> <mailto:<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>> wrote:<br>
>>>>><br>
>>>>> A question about the new #unknown behavior. Is it intended to be used for error handling too ?<br>
>>>>> Will it be possible to use in catch clause ?<br>
>>>> If we go with the #unknown approach, then yes of course it will work in catch clauses. They are patterns, so it<br>
>>>> naturally falls out.<br>
>>>> If we go with the “unknown default:” / “unknown case:" approach, then no, this has nothing to do with error handling.<br>
>>>> IMO, this pivots on the desired semantics for “unknown cases in enums”: if you intentionally try to match on this,<br>
>>>> do we get a warning or error if you don’t handle all the cases? If we can get to consensus on that point, then the<br>
>>>> design is pretty obvious IMO.<br>
>>><br>
>>> For me the other question is what "all the cases" means for enum with private cases(if we'll have them). I.e. if<br>
>>> switch contains all the "public" cases of frozen enum - does this mean "all the cases" were processed? As I<br>
>>> understand, the answer is no, because we *can* have 'private' case value here and so we need to react to this. How<br>
>>> switch will look in this case?<br>
>>><br>
>>> switch frozenEnumWithPrivateCases {<br>
>>> case .one: ..<br>
>>> case .two: ..<br>
>>> unknown default: .. // or 'case #unknown:' depending on our decision, or 'unknown case:' etc<br>
>>> }<br>
>>> ?<br>
>>> But then such switch looks exactly as switch for non-frozen enum value, no? It looks like we are reacting on future<br>
>>> new cases, while enum is frozen.<br>
>>><br>
>>> Moreover. How the switch for non-frozed enum with private cases should looks like?<br>
>>><br>
>>> switch nonfrozenEnumWithPrivateCases {<br>
>>> case .one: ..<br>
>>> case .two: ..<br>
>>> unknown default: .. // or 'case #unknown:' depending on our decision, or 'unknown case:' etc<br>
>>> }<br>
>>> ? But then, is that 'unknown default' for reacting on "future" cases we didn't know about during the compilation OR<br>
>>> it is for reacting on private cases?<br>
>>><br>
>>> Or the main idea that we don't want to separate "future" cases and "private" cases?<br>
>><br>
>> I think treating both as the same thing is the right idea. You also need to handle "future private" cases and "private<br>
>> cases that become public in the future". These are all unknown cases in the context of the switch.<br>
>><br>
>> So an enum with private cases can't be switched exhaustively outside of its module. Thus, @frozen would need to forbid<br>
>> private cases... or we need @exhaustive to forbid private cases so they can be allowed by @frozen.<br>
><br>
> As mentioned in "Future directions", my recommendation to anyone planning to write a proposal for non-public cases is to<br>
> go with the former, which would keep it from infecting the design.<br>
><br>
<br>
Thank you for the comment!<br>
From proposal:<br>
"Were such a proposal to be written, I advise that a frozen enum not be permitted to have non-public cases."<br>
<br>
OK. Seems logically for frozen enum(imported from another module) to not have non-public cases, as such cases most<br>
likely will be added later during the evaluation of the library(external module) - so such enum should not be frozen then.<br>
<br>
I'd like to discuss how current decision will fit into the (possible) future 'private cases' in enum.<br>
<br>
1. Non-frozen enum with private cases in the same module.<br>
<br>
It seems like we'll need to write<br>
switch val {<br>
case .one : ..<br>
case .two : ..<br>
unknown default: .. // for private cases, even 'val' can't have 'future' cases<br>
}<br>
<br>
So, 'unknown default' will mean not just 'future cases', but 'future cases and private cases'.<br>
<br>
2. Non-frozen enum with private cases in another module.<br>
<br>
In this case, if we want exhaustive switch, we need to use 'unknown default'. But I feel like known but private cases<br>
are not the same as future public cases for the 'consumer' of that enum, no?<br>
<br>
I mean, when making a decision what to do inside 'unknown default' - isn't it important to know what is the "event" -<br>
new public case or private(even "known") case? I'm thinking about something like this:<br>
<br>
let val = getSomeNonFrozenEnumResultFromLibrary()<br>
switch val {<br>
case .one : ... // process the val on our side<br>
case .two : ... // process the val on our side<br>
<br>
private default : sendValueBackToLibraryToProcess(val) // not interested, calling some special handler<br>
future default : .. // somehow react on important new case introduced in library. for example, show "please update<br>
the app" for the user and cancels the current operation<br>
}<br>
<br>
I don't think we need to think about "future private" cases, as we don't have access to them in any case, so current and<br>
future private cases are not distinguishable for us.<br>
<br>
I'm not sure if we should distinct future cases and private cases on 'switch' side, but I think we should discuss this<br>
now for taking a correct decision regarding 'unknown default' etc.<br>
<br>
P.S. FWIW I agree with Chris Lattner that 'unknown default' fits into Swift syntax and mental model much better than<br>
'unknown case'. This handler is for default reacting on _number_ of unknown cases, not for reacting on some _specific_<br>
case, like other 'case xxx:' handlers. Are we going to discuss this and select the better name? Or most of us not agree<br>
that 'unknown case' is the best?<br>
<br>
Vladimir.<br>
<br>
> Jordan<br>
><br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>