[swift-evolution] Proposal: Pattern Matching Partial Function (#111)
Thorsten Seitz
tseitz42 at icloud.com
Tue Feb 9 00:00:23 CST 2016
> Am 09.02.2016 um 01:51 schrieb Dany St-Amant via swift-evolution <swift-evolution at swift.org>:
>
>
>> Le 8 févr. 2016 à 10:54, Thorsten Seitz <tseitz42 at icloud.com> a écrit :
>>
>>
>>
>>> Am 07.02.2016 um 16:47 schrieb Dany St-Amant via swift-evolution <swift-evolution at swift.org>:
>>>
>>> Assuming this implicit return part of this proposal get generalized, could we instead of the dedicated match function have a generic way to feed the parameters to the closure at the start, where it would make sense for the desired switch usage.
>>>
>>> let str:String = (state) -> { switch $0 { case .Cold: "Too Cold"; case .Hot: "Too Hot"; default: "Just right" } }
>>
>> That's easy, just use the match() function from the proposal:
>>
>> let str:String = match(state) { switch $0 { case .Cold: "Too Cold"; case .Hot: "Too Hot"; default: "Just right" } }
>>
>> No new syntax needed for that.
>
> As/if the implicit returns is generalized, I feel that match is bit out of place in the for nested if. I should have provided an example to clarify my thought. Here’s one, where the :? do a better job, but this thread has its origin in came people not liking this sometime cryptic operator (which I like to use)
I like :? too, btw.
> str = major > 0 ? "Major" : minor > 0 ? "Minor" : "None"
>
> which is ill suited for switch case but doable.
No need to press something into switch which does not fit.
The proposal is not about using switch for everything and it is not about replacing the ternary operator. It is more about making a switch expression available.
> switch (major,minor) {
> case (_, _) where major > 0: str="Major"
> case (_, _) where minor > 0: str="Minor"
> default: str="None"
> }
Rewriting a little bit I don't think switch looks that bad even for your example:
switch (major,minor) {
case _ where major > 0: "Major"
case _ where minor > 0: "Minor"
default: "None"
}
> Assuming that the implicit returns is generalized to all closures (and even functions like getter), of course only for the one matching my safety rule (in short single statement/function call/exhaustive if or switch). The if can be express as, using the new global match function:
>
> str = match(major,minor){ if $0 > 0 { "Major" } else if $1 > 0 { "Minor" } else { "None" } }
>
Making `if` into an expression (which would be a requirementbhere) has been more or less ruled out by Chris Lattner.
>
> or using normal inline closure calls:
>
> str = { if $0 > 0 { "Major" } else if $1 > 0 { "Minor" } else { "None" } }(major,minor)
>
> versus the feeding I wonder about:
>
> str = (major,minor) -> { if $0 > 0 { "Major" } else if $1 > 0 { "Minor" } else { "None" } }
>
> Maybe it’s just me, but the match function feels a bit weird, in the first version.
I agree. That's why it is accompanied by the partial function closure syntax which effectively results in a switch or rather match expression.
-Thorsten
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160209/d0513e86/attachment.html>
More information about the swift-evolution
mailing list