User-defined "opIs"
Meta via Digitalmars-d
digitalmars-d at puremagic.com
Wed Sep 24 16:29:23 PDT 2014
On Wednesday, 24 September 2014 at 23:08:26 UTC, Adam D. Ruppe
wrote:
> On Wednesday, 24 September 2014 at 22:48:36 UTC, Meta wrote:
>> Is this supposed to work, and if not, should an enhancement be
>> made to allow it?
>
> It is not supposed to work - the docs don't list is as
> overridable (http://dlang.org/expression.html look for
> "Identity expressions") - and I don't think it should work
> either. Consider:
>
> class A { opIs() }
>
> if(a is null)
>
> If that were rewritten into if(a.opIs(null))... and a were
> null, you'd have some trouble. Putting in an automatic null
> pre-check would start to complicate that is currently a simple
> operator.
I'm thinking more for structs, which can't be null. The only
other option right now is to use alias this, but that won't work
in Proxy's case. For classes, I agree it's tricky... I know we're
trying to *remove* things from Object, but what if we added:
class Object
{
static bool opIs(T, U)(inout(T) a, inout(U) b) inout
{
//Some implementation
}
}
And calls to is on an object get rewritten as:
Object.opIs!(typeof(a), typeof(b))(a, b)
So `if (a is null)` becomes `if (Object.opIs(a, null))` only when
a is a class. When it's a struct, it would just be rewritten as
`<struct name>.opIs!T(val)`.
More information about the Digitalmars-d
mailing list