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

Craig Cruden ccruden at novafore.com
Sat Jan 30 13:15:09 CST 2016


The case partial functions are available in all closures, plus you still have the option to write your syntax based on the proposal with the only difference being the keyword of match vs switch.

let str = match(state) {
	case .Cold: “Too cold”
	case .Hot: “Too hot”
	default: “now it is just boring”
}

match just being a function that takes a value and a closure.




> On 2016-01-31, at 2:11:20, Radosław Pietruszewski via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I certainly would prefer if we could use switch (and if/else for that matter) as an expression, instead of some magic syntax usable in `map`. so:
> 
> let str = switch(state) {
>         case .Cold:  "Too cold"
>         case .Hot:    "Too hot"
>         default:        "Just right"
>     }
> 
> 
> — Radek
> 
>> On 29 Jan 2016, at 08:10, Paul Cantrell via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> 
>>> On Jan 29, 2016, at 1:01 AM, Charles Constant via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> 
>>> I'm still +1 on this.
>>> 
>>> I would very much enjoy being able to do this:
>>> 
>>> let str = state.map {
>>> 	case .Cold: 	“Too cold”
>>> 	case .Hot: 	“Too hot”
>>> 	default: 	“Just right”
>>> }
>> 
>> 
>> The language currently allows this:
>> 
>>     let str: String
>>     switch(state) {
>>         case .Cold: str = "Too cold"
>>         case .Hot:  str = "Too hot"
>>         default:    str = "Just right"
>>     }
>> 
>> While that’s slightly noisier, I’m not convinced the minor additional concision justifies the substantial additional language complexity. I’m not even convinced the new syntax is clearer so much as cleverer.
>> 
>> I’d need to see a much more compelling example to be in favor of this proposal.
>> 
>> Cheers,
>> 
>> Paul
>> 
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> _______________________________________________
> 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/20160131/6f2d9155/attachment.html>


More information about the swift-evolution mailing list