Can someone fill me in as to how this differs from (0…4).contains(x)? Personally I don’t like the choice for the operator, and think it’s not very clear, if we can do the same thing with a method then I’d prefer removing the operator to be honest.
> On 7 Apr 2016, at 12:57, David Rodrigues via swift-evolution > <[email protected]> wrote: > > Hi all, > > Swift has a pattern match operator, ~=, which is unknown to many (like me > until a few weeks ago), that performs a match between a value and a certain > pattern, e.g. checking if an integer value is contained in a range of > integers. > > This operator may be little known, but it plays a key role in the language > since it's used behind the scenes to support expression patterns in `switch` > statement case labels, which we all know are extremely popular. > > let point = (2, 4) > switch point { > case (0, 0): > print("The point is at the origin") > case (0...4, 0...4): > print("The point is in the subregion") > default: > break > } > > Most of the time we don't use the operator directly but it is available and > can be handy in certain conditions. > > let point = (2, 4) > switch point { > case (let x, let y) where 0...4 ~= x && 0...4 ~= y: > print("The point is in the subregion") > default: > break > } > > However the current syntax is not ideal (in my opinion). We're not really > declaring the operation that we want to do, and that has an impact in the > expressivity and readability of the code. Currently we're doing matches like > "if blue is the ocean" instead of "if the ocean is blue" or "if the ocean > contains the whale" instead of "if the whale is in the ocean". > > For that reason, I would like to suggest inverting the order of the operator > to match more closely our logical thought. > > case (let x, let y) where x =~ 0...4 && y =~ 0...4: // Proposed > // vs > case (let x, let y) where 0...4 ~= x && 0...4 ~= y: // Current > > I have an ongoing proposal to suggest this change and it contains a little > more context. It is available here: > > https://github.com/dmcrodrigues/swift-evolution/blob/proposal/invert-order-of-pattern-match-operator/proposals/NNNN-invert-order-of-pattern-match-operator.md > > <https://github.com/dmcrodrigues/swift-evolution/blob/proposal/invert-order-of-pattern-match-operator/proposals/NNNN-invert-order-of-pattern-match-operator.md>. > > Any feedback is very welcome. > > Thank you. > > David Rodrigues > _______________________________________________ > swift-evolution mailing list > [email protected] > https://lists.swift.org/mailman/listinfo/swift-evolution
_______________________________________________ swift-evolution mailing list [email protected] https://lists.swift.org/mailman/listinfo/swift-evolution
