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

Dany St-Amant dsa.mls at icloud.com
Mon Feb 8 18:51:11 CST 2016


> 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 <mailto: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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160208/72bc2979/attachment.html>


More information about the swift-evolution mailing list