August 03, 2015 !in operator | ||||
---|---|---|---|---|
| ||||
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 |
August 03, 2015 Re: !in operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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
|
Copyright © 1999-2021 by the D Language Foundation