Extract Payload for enum cases having associated value

I‘d like to propose a different solution: what about extending the syntax of anonymous functions to enable pattern matching on its arguments, i.e.

{ case .foo(let val) in val }

This would pattern match the argument and in the matching case return the result of the function wrapped in .some (i.e. .some(val)) and in the non matching case return nil.

This would keep all the safety and power of pattern matching, including elegant handling of multiple parameters where I might only be interested in some of them and nestings, e.g.

{ case .foo(_, .baz(let x, _), let y) in x * y }

-Thorsten

1 Like