!in operator

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 3 01:27:47 PDT 2015


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




More information about the Digitalmars-d-learn mailing list