I'd prefer to offer both ~= and =~, allowing the consumer to choose which side 
the pattern sits on.

-- E


> On Apr 7, 2016, at 11:09 AM, David Owens II via swift-evolution 
> <[email protected]> wrote:
> 
> It would be nice to know the rationale behind the choice of the current 
> syntax. I agree that these seem more natural:
> 
> @warn_unused_result
> public func ~=<I : ForwardIndexType where I : Comparable>(value: I, pattern: 
> Range<I>) -> Bool
> 
> @warn_unused_result
> public func ~=<I : IntervalType>(value: I.Bound, pattern: I) -> Bool
> 
> I would not change from `~=` to `=~` though.
> 
> So you have this:
> 
> let x = 4
> 
> switch x {
> case let v where x ~= 0...5: print("matched: \(v)")
> default: print("nothing!!")
> }
> 
> -David
> 
>> On Apr 7, 2016, at 4:57 AM, David Rodrigues via swift-evolution 
>> <[email protected] <mailto:[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] <mailto:[email protected]>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> _______________________________________________
> 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

Reply via email to