[swift-evolution] [Proposal] Invert the order of pattern match operator
David Rodrigues
david.ndh at gmail.com
Thu Apr 7 14:11:28 CDT 2016
> On 07 Apr 2016, at 19:16, Dave Abrahams via swift-evolution <swift-evolution at swift.org> 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
>
> +1
This operator was firstly introduced in Swift in the form =~ but then it was reversed to the current form (~3y ago). There’s a little context about this change on Twitter: https://twitter.com/dmcrodrigues/status/717063623957471232 <https://twitter.com/dmcrodrigues/status/717063623957471232>.
>> I would not change from `~=` to `=~` though.
>
> One downside with `~=` is that it reads like `+=`, which mutates the
> LHS. Of course, `=~` has its own issues, e.g. `x=~y` might read as `x =
> ~y`.
I agree but this operator already exists in other languages like Ruby and Perl, so align the syntax may be a good option.
> On 07 Apr 2016, at 18:56, Erica Sadun <erica at ericasadun.com> wrote:
>
> I'd prefer to offer both ~= and =~, allowing the consumer to choose which side the pattern sits on.
On the other hand, I like in particular the option of having both `~=` and `=~` to provide more flexibility like Erica has suggested. The main question is if that’s ok introduce another operator in the language.
—
David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160407/4d8c87f6/attachment.html>
More information about the swift-evolution
mailing list