User-defined "opIs"

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 25 05:15:14 PDT 2014


On 9/24/14 6:48 PM, Meta wrote:
> The following code fails under DMD 2.065:
>
> struct Test
> {
>      bool opBinary(string op: "is", T: typeof(null))(T val)
>      {
>          return false;
>      }
>
>      bool opBinaryRight(string op: "is", T: typeof(null))(T val)
>      {
>          return false;
>      }
> }
>
> void main()
> {
>      auto t = Test();
>      //Error: incompatible types for ((t) is (null)): 'Test' and
> 'typeof(null)'
>      //assert(t !is null);
>      //assert(null !is t);
> }
>
> Is this supposed to work, and if not, should an enhancement be made to
> allow it? This is stopping std.typecons.Proxy from being a true proxy.
> See the following:
>
> struct Test
> {
>      int* ptr;
>      mixin Proxy!ptr;
>
>      this(int* p)
>      {
>          ptr = p;
>      }
> }
>
> auto i = new int;
> assert(i !is null);
> auto t = Test(i);
> //Error: incompatible types for ((t) !is (null)): 'Test' and 'typeof(null)'
> //assert(t !is null);

You're looking at it the wrong way. 'is' is not overridable, and 
shouldn't be.

But the type of null should be overridable (or you should be able to 
tell the compiler "this type can be compared with null"). I believe 
there has been discussion on this in the past, can't remember where it led.

-Steve


More information about the Digitalmars-d mailing list