digitalmars.D - default cast in opCast
- Basile B. (36/36) Dec 29 2025 D could define that `this` cast within an `opCast` should
- Adam D. Ruppe (6/9) Dec 29 2025 try
- Basile B. (4/13) Dec 29 2025 Well said but TBF, I forgot template constraints even exist.
- Steven Schveighoffer (6/10) Dec 31 2025 cast(T)cast(Object)this
- Paul Backus (23/28) Dec 31 IMO a better solution would be to fall back to the default cast
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
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
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: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.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
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
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









Basile B. <basile.b gmx.com> 