www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - !in operator

reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Aug 02 2015
parent Jonathan M Davis via Digitalmars-d-learn writes:
On Sunday, August 02, 2015 21:51:48 Ali Çehreli via Digitalmars-d-learn wrote:
 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
It would make no sense to be able to overload !in directly given D's philosophy on operator overloading. key !in foo has to be the same as !(key in foo) for consistency. It's the same as why opEquals, opCmp, op!"++", and op!"--" are all used to overload multiple operators. - Jonathan M Davis
Aug 03 2015