default cast in opCast
Paul Backus
snarwin at gmail.com
Thu Jan 1 02:28:55 UTC 2026
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
}
```
More information about the Digitalmars-d
mailing list