5 ms·
Arguably C++ is worse: class Equal { public: template <typename T> bool operator==(const T& eq) { return true; }; All kind
by Bootvis 9y ago
Arguably C++ is worse:
class Equal {
public:
template <typename T>
bool operator==(const T& eq) { return true; };
All kinds of equality are possible.
- labster 9y agoIn Perl 6, we can do much the same without all of the boilerplate. class a {}; multi sub infix:<==> (a $a, $b) { True } if a == 1 && a == 2 && a == 3 { say "true!" } If you wanted to make an actual iterator out of `a`, you could do: multi sub infix:<==> (a $a, $b) { ++(state $) == $b } In that case, `a` would equal 1, 2, and 3 in that order. 5 is right out.
- perlgeek 9y agoOr you could just use a junction: my \a = any(1, 2, 3); if ( a == 1 && a == 2 && a == 3 ) { say "OMG quantum superposition!!!"; } For those wondering what's going on: https://docs.perl6.org/type/Junction.html https://docs.perl6.org/type/Junction.html :-)
- paulirwin 9y agoIt's also trivial in C# to overload the == operator. The following works in C# Interactive (the Visual Studio REPL). class A { public static bool operator ==(A a, int b) => true; public static bool operator !=(A a, int b) => false; } var a = new A(); if (a == 1 && a == 2 && a == 3) Console.WriteLine("This works");
- contravariant 9y agoThe only thing bad about that is that there's no check whether equality is symmetric. As written it does make sense if you interpret it as "<=" or ">=".