[Issue 6777] alias this disables casting for classes

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Oct 10 15:45:09 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=6777

--- Comment #13 from Steven Schveighoffer <schveiguy at yahoo.com> ---
(In reply to Walter Bright from comment #12)
> The problem comes from the idea that having an alias this means that
> operations not supported by the class get forwarded to the alias this. Since
> opCast() can override what explicit casts do for a class reference, the
> alias this needs to be respected (even if it doesn't provide an opCast(), it
> still can be cast).

But the only mechanism to use dynamic casting is to cast to the requested type.
This is expected functionality in the class instance, and in my opinion,
default functionality should override any alias this functionality.

Same thing with cast(void *). This is expected functionality.

I don't know of any other mechanism that overrides existing functionality from
an alias this'd type, especially when that functionality is not intentionally
specified.

For example, I would be really surprised if you had:

struct S
{
   int x;
}

struct T
{
   int y;
   S s;
   alias s this;
}

auto t = T(5, S(2));

and this failed because it's trying to call the S implicit constructor instead
of the T implicit constructor.

--


More information about the Digitalmars-d-bugs mailing list