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

Paul Ossenbruggen possen at gmail.com
Fri Jan 29 01:44:35 CST 2016


Hi Paul, 

Your example, can’t use type inference to determine the type of str which feels clumsy, and “str =“ is duplicated over and over. You also have no guarantee that str get assigned in each case. While this example that error is not likely, if your cases are more complex that could become a problem. Also, if I am writing something that is functional, it is less likely, as Charles points out, another developer will stick some side effect based code in your cases. Finally, for the proposal you could write it even more compactly: Switch statements seem much more heavyweight. 

let str = match(state) { cases .Cold: “Too cold”, .Hot: “Too hot”
			default: “Just right” }

Cheers,
- Paul


> On Jan 28, 2016, at 11:37 PM, Charles Constant via swift-evolution <swift-evolution at swift.org> wrote:
> 
> @Paul
> 
> On the more objective side of things, there is also the fact that, as the proposal states, it's nice to have protection against this happening:
> 
>     let str: String
>     switch(state) {
>         case .Cold: str = "Too cold"; self.freezer.stop()
>         case .Hot:  str = "Too hot"
>         default:    str = "Just right"
>     }
> 
> _______________________________________________
> 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/20160128/22d8b9a7/attachment.html>


More information about the swift-evolution mailing list