[swift-evolution] ternary operator ?: suggestion
Thorsten Seitz
tseitz42 at icloud.com
Tue Jan 19 17:17:39 CST 2016
> Am 19.01.2016 um 19:28 schrieb Chris Lattner <clattner at apple.com>:
>
> On Jan 19, 2016, at 10:03 AM, Thorsten Seitz <tseitz42 at icloud.com <mailto:tseitz42 at icloud.com>> wrote:
>>
>>> Am 19.01.2016 um 06:28 schrieb Chris Lattner via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>>:
>>>
>>> If you extend the same analogy to switch, then the most important cases are when the pattern being matched and the values being processed are lexically small, and have few cases. We have a lot of syntactic sugar for processing optionals (e.g. if/let, the ?? operator, etc), but ?? for example doesn’t apply to general pattern matching. With the expression above, for example, you could match on an enum analogously to the ?? operator like this:
>>>
>>> result = someEnum ? case .SomeCase(let x): x, default: y
>>>
>>> If you compare that to a switch statement, I can see how that could be compelling. OTOH, the larger the expression (the more cases) and the more complex the patterns, the better a switch statement starts to look.
>>
>> For me the killer feature is not being able to write it inline, it is making it clear that an assignment happens, i.e.
>>
>> let result = someEnum ?
>> case .SomeCase(let x): x
>> default: y
>>
>> is IMO much more readable (= allowing to instantly see what’s going on) than
>>
>> let result
>> switch someEnum {
>> case .SomeCase(let x): result = x
>> default: result = y
>> }
>
> Yes, I agree with you.
>
>> For the same reason
>>
>> let result = match(someEnum) {
>> case .SomeCase(let x): x
>> default: y
>> }
>>
>> would be just as appealing to me. For using this as a switch-expression I’m strongly against introducing a special map method for basic types, though and rather propose to use a global function „match“ (more on that further below).
>>
>> The inline version (if desired) is not too different from the ?-based switch-expression:
>>
>> let result = match(someEnum) { case .SomeCase(let x): x; default: y }
>
> Yes, after reading this again, I agree that just taking ‘match’ (or something like it) as a keyword and making it be a proper switch expression (with braces) is probably the right way to go. Overloading ?: is gross :-)
Reusing the "?“ would not require a new keyword and look similar (and very lightweight) to the ternary operator, underlining its expression character. I don’t think that’s bad.
>> The big advantage of the partial function proposal would be that it is much more general as it is usable everywhere a unary function argument is used, i.e. for map etc.
>
> Sure, you’re optimizing for one specific case at the expense of composability. I personally don’t find that compelling, but YMMV.
I’m not sure I understand. What specific case do you mean and what composability is lost with the unary function approach?
The syntax with a global match() function would look just the same as your suggestion of introducing a switch expression with a match keyword, but the unary function syntax could be used in other places as well (like the map example).
> We’re somewhat unlikely to add a single syntax for switch expressions, but I find it inconceivable that we would add partial functions AND switch expressions. This isn’t a case where “both are potentially interesting, so lets have both” - this is a situation where we (as a community) would have to pick one or the other.
I agree.
-Thorsten
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160120/e4975f9b/attachment.html>
More information about the swift-evolution
mailing list