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

Howard Lovatt howard.lovatt at gmail.com
Sat Jan 30 19:35:01 CST 2016


If values and objects were made Collections of exactly one value then you
could write:

  let str = state.flatMap {
      switch $0 {
          case .Cold: return "Too cold"
          case .Hot: return "Too hot"
          default: return "now it's just boring"
      }
  }

This would be purely a library solution which is a lot easier to implement
and has the advantage of then giving real use data to see if people
actually use the facility.

On Sunday, 31 January 2016, Craig Cruden via swift-evolution <
swift-evolution at swift.org> wrote:

> 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
> <javascript:_e(%7B%7D,'cvml','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
> <javascript:_e(%7B%7D,'cvml','swift-evolution at swift.org');>> wrote:
>
>
> On Jan 29, 2016, at 1:01 AM, Charles Constant via swift-evolution <
> swift-evolution at swift.org
> <javascript:_e(%7B%7D,'cvml','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
> <javascript:_e(%7B%7D,'cvml','swift-evolution at swift.org');>
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> <javascript:_e(%7B%7D,'cvml','swift-evolution at swift.org');>
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>

-- 
  -- Howard.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160131/222126df/attachment.html>


More information about the swift-evolution mailing list