What's the deal with all the different Perl 6 equality operators? (==, ===, eq, eqv, ~~, =:=, …)

前端 未结 4 1027
臣服心动
臣服心动 2021-01-31 08:15

Perl 6 seems to have an explosion of equality operators. What is =:=? What\'s the difference between leg and cmp? Or eqv and

4条回答
  •  生来不讨喜
    2021-01-31 08:50

    Does the summary in Synopsis 3: Comparison semantics do what you want, or were you already reading that? The design docs link to the test files where those features are used, so you can see examples of their use and their current test state.

    Perl 6's comparison operators are much more suited to a dynamic language and all of the things going on. Instead of just comparing strings or numbers (or turning things into strings or numbers), now you can test things precisely with an operator that does what you want. You can test the value, the container, the type, and so on.

    In one of the comments, you ask about eqv and cmp. In the old days of Perl 5, cmp was there for sorting and returns one of three magic values (-1,0,1), and it did that with string semantics always. In Perl 6, cmp returns one of three types of Order objects, so you don't have to remember what -1, 0, or 1 means. Also, the new cmp doesn't force string semantics, so it can be smarter when handed numbers (unlike Perl 5's which would sort like 1, 10, 11, 2, 20, 21 ...).

    The leg (less than, equal, greater than) is cmp with string semantics. It's defined as Perl 6's ~$a cmp ~$b, where ~ is the new "string contextualizer" that forces string semantics. With leg, you are always doing a string comparison, just like the old Perl 5 cmp.

    If you still have questions on the other operators, let's break them down into separate questions. :)

提交回复
热议问题