!in operator

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 2 21:51:48 PDT 2015


Is my understanding below correct? Does any documentation need updating?

Operator precedence table lists !in as an operator:

   http://wiki.dlang.org/Operator_precedence

Operator overloading documentation does not mention it:

   http://dlang.org/operatoroverloading.html#binary

However, 'a !in b' seems to be lowered to '!(a in b)'. It is possible to 
define "!in" but it is never called:

struct S
{
     bool opBinaryRight(string op)(int i) const
         if (op == "in")
     {
         import std.stdio;
         writeln("in");
         return true;
     }

     bool opBinaryRight(string op)(int i) const
         if (op == "!in")
     {
         // Never called
         assert(false);
         return false;
     }
}

void main()
{
     auto s = S();
     assert(42 in s);
     assert(!(42 !in s));
}

The "in" overload gets called twice:

in
in

Ali


More information about the Digitalmars-d-learn mailing list