[swift-evolution] Proposal: Pattern Matching Partial Function (#111)

Maximilian Hünenberger m.huenenberger at me.com
Tue Feb 9 05:31:44 CST 2016


The ternary expression can also be accomplished by this match:

    str = match((major > 0, minor > 0)) {
        case (true, _): "Major"
        case (_, true): "Minor"
        default: "None"
    }

Or using "cases":

    str = match((major > 0, minor > 0)) {
        cases (true, _): "Major", (_, true): "Minor"
        default: "None"
    }

At least the former version is to me clearer than the original ternary expression.
While the switch expression "forces" you to format the code whereas a ternary operator doesn't.

"correctly" formatted:

    str = major > 0 ? "Major" :
             minor > 0 ? "Minor" : "None"

But as Thorsten has already said the proposal is not about removing the ternary operator.

- Maximilian

> 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)
> 
>     str = major > 0 ? "Major" : minor > 0 ? "Minor" : "None"
> 
> which is ill suited for switch case but doable.
> 
>     switch (major,minor) {
>         case (_, _) where major > 0: str="Major"
>         case (_, _) where minor > 0: str="Minor"
>         default: str="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" } }
> 
> 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.
> 
> Dany
> 
> 
> _______________________________________________
> 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/20160209/247c3ab3/attachment.html>


More information about the swift-evolution mailing list