www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - default cast in opCast

reply Basile B. <basile.b gmx.com> writes:
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 ?
Dec 29 2025
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 29 December 2025 at 20:29:16 UTC, Basile B. wrote:
     auto opCast(T)()
     {
         static if (is(T == uint))
try auto opCast(T : uint)() { return member; } isntead of all that. then the opCast only ever matches uint.
Dec 29 2025
parent Basile B. <basile.b gmx.com> writes:
On Monday, 29 December 2025 at 20:52:47 UTC, Adam D. Ruppe wrote:
 On Monday, 29 December 2025 at 20:29:16 UTC, Basile B. wrote:
     auto opCast(T)()
     {
         static if (is(T == uint))
try auto opCast(T : uint)() { return member; } isntead of all that. then the opCast only ever matches uint.
Well said but TBF, I forgot template constraints even exist. Anyway casting `this` inside the body of opCast is so suspicious that at least the messages should be better.
Dec 29 2025
prev sibling next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On Monday, 29 December 2025 at 20:29:16 UTC, Basile B. wrote:
 D could define that `this` cast within an `opCast` should 
 fallback to the default, i.e that ignores the operator 
 overload. Example

 [...]
cast(T)cast(Object)this And you can probably avoid the dynamic cast by checking if implicit cast works first. Though I think Adam’s solution is better. -Steve
Dec 31 2025
prev sibling parent Paul Backus <snarwin gmail.com> writes:
On Monday, 29 December 2025 at 20:29:16 UTC, Basile B. wrote:
 D could define that `this` cast within an `opCast` should 
 fallback to the default, i.e that ignores the operator overload.
[...]
 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.
IMO a better solution would be to fall back to the default cast behavior if no overload of `opCast` matches for a given cast expression. So, for example: ```d class C {} class B : C { uint member = 8; T opCast(T)() if (T == uint) // only match for cast(uint) { return member; } } void main() { auto c = new C; assert(cast(uint) c == 8); B b = cast(B) c; // no match; fall back to default } ```
Dec 31