default cast in opCast

Basile B. basile.b at gmx.com
Mon Dec 29 20:29:16 UTC 2025


D could define that `this` cast within an `opCast` should 
fallback to the default, i.e that ignores the operator overload. 
Example

```d
module runnable;

class B
{

}

class C : B
{
     uint member = 8;
     auto opCast(T)()
     {
         static if (is(T == uint))
             return member;       // infer typeof(member)
         else
             return cast(T) this; // want to fallback to default 
casts
                                  // consequently infer T and skip 
the opover
     }
}

void main()
{
     scope c = new C;
     assert( (cast(uint) c) == 8 ); // OK
     assert(  cast(B) c);           // plenty of errors
}
```

The problem D has right now is that everyeach possible case must 
be handled, there's no way to fallback to the default cast-exp 
semantics.

But there's no breakage actually... you just avoid the compiler 
to fall into infinite lowerings 
(`c.opCast!(T).opCast!(T).opCast!(T).opCast!(T)` etc.

what do you think ?


More information about the Digitalmars-d mailing list