rfcs icon indicating copy to clipboard operation
rfcs copied to clipboard

Pattern matching that supports equality

Open CaseyLeask opened this issue 11 years ago • 10 comments

Hi all,

I was going through http://rustbyexample.com/flow_control/match/guard.html

fn main() {
    let pair = (2, -2);
    // TODO ^ Try different values for `pair`

    println!("Tell me about {:?}", pair);
    match pair {
        (x, y) if x == y => println!("These are twins"),
        // The ^ `if condition` part is a guard
        (x, y) if x + y == 0 => println!("Antimatter, kaboom!"),
        (x, _) if x % 2 == 1 => println!("The first one is odd"),
        _ => println!("No correlation..."),
    }
}

and I tried to do https://gist.github.com/CaseyLeask/89d85dd213c618dac58d#file-guard-rs-L7 .

fn main() {
    let pair = (2, -2);
    // TODO ^ Try different values for `pair`

    println!("Tell me about {:?}", pair);
    match pair {
        (x, x) => println!("These are twins"),
        // The ^ `if condition` part is a guard
        (x, y) if x + y == 0 => println!("Antimatter, kaboom!"),
        (x, _) if x % 2 == 1 => println!("The first one is odd"),
        _ => println!("No correlation..."),
    }
}

Is there a reason that this wouldn't be desirable?

Cheers, Casey

CaseyLeask avatar Apr 12 '15 12:04 CaseyLeask

Sorry if this is a duplicate, but I couldn't find this question in either the rust or rfc issues.

CaseyLeask avatar Apr 12 '15 12:04 CaseyLeask

Other languages which have pattern matching don't allow this.

In Haskell, this code doesn't compile:

f x x = 1
f x y = 0

But this code does

f x y | x == y = 1
      | otherwise = 0

IMO, pattern matching should be a form of destructuring, not running functions like equality, so, I don't think that this is a good feature to add.

ghost avatar Apr 13 '15 01:04 ghost

@Undeterminant As a counterexample to that, Erlang is another language which does.

-module(equality).
-export([eq/2]).

eq(X, X) -> true;
eq(_, _) -> false.

Since we're talking about pattern matching, I think equality is a pattern that could be detected.

CaseyLeask avatar Apr 13 '15 01:04 CaseyLeask

The issue with supporting equality in patterns in general is that its ambitious what kind of equality is meant - Does it require a Eq or PartialEq implementation, or does it use its own definition of structural equality?

Kimundi avatar Apr 13 '15 09:04 Kimundi

This requires special casing _ even more, because eq a a = True and eq _ _ = True are α-equivalent.

I prefer the current, shadowing behaviour.

nagisa avatar Apr 13 '15 11:04 nagisa

I would argue that no pattern without a guard should ever desugar to a pattern with a guard, because patterns with a guard do not influence exhaustiveness checks and reachability checks for later match arms. That is, Foo(x, x) and Foo(x, y) if x == y are not semantically equivalent, because the former must involve a "structural" constraint that influences the various match checks, whereas the latter is forbidden from being anything but a run-time check.

(Full disclaimer: I have a draft RFC on the "in a month or two" horizon that depends on this sort of distinction, because it proposes using match patterns to influence coherence checks involving dependent types.)

That's not necessarily an argument against allowing Foo(x, x). Rather, I'm saying that if we do allow such a thing, it needs to be with the understanding that it is actually adding a constraint that the compiler needs to track. I.e., if a match pattern uses Foo(x, x), then a later pattern that uses something like Foo(None, None) in the same position should be considered unreachable.

quantheory avatar Apr 14 '15 04:04 quantheory

I am strongly against implementing this. As said above, this generates a very subtle (and possibly expensive) equality check. Maybe this could make sense when restricted to Copy types, but I really don't see an issue with using a match guard. I also don't really think this is unambiguous enough for the behavior to be obvious: if I saw a pattern like this, I might suspect that it tested, but not with anything close to certainty, which means it's not only another language feature to implement, but also another language feature to explain. It doesn't seem like a very common use case to me, and the cost of not having this feature is low, even when that use case comes up, so I'm strongly in favor of keeping the language simpler.

bfops avatar Nov 19 '15 17:11 bfops

Closing in favor of tracking issue https://github.com/rust-lang/rfcs/issues/1059.

Centril avatar Oct 07 '18 15:10 Centril

@Centril your link seems to be wrong, it points to this same issue.

golddranks avatar Oct 07 '18 18:10 golddranks

Apparently I need some sleep... I meant to link it to: https://github.com/rust-lang/rfcs/issues/263.

Centril avatar Oct 07 '18 18:10 Centril