opDispatch and alias this

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Jun 26 00:56:13 UTC 2018


On Monday, June 25, 2018 23:13:12 Seb via Digitalmars-d wrote:
> Apparently three years ago it was we decided to ban alias this
> and opDispatch in the same class.
> What are your thoughts on this now?
> Is anyone depending on using alias this + opDispatch together
> like e.g. in https://github.com/dlang/phobos/pull/6596?

I think that that choice made a lot of sense, but regardless, I think that
the only reason that the issue of whether having alias this and opDispatch
on the same type has anything to do with Final is because if we make it
illegal, then Final has to be fixed so that it only does one, whereas if we
don't make it illegal, then presumably, we can leave Final as-is (assuming
that having both opDispatch and alias this on Final doesn't result in subtle
bugs, which it may).

Either way, conceptually, what Final is doing should use alias this. It's
trying to implement head-const, and it really doesn't make sense to require
an explicit cast to convert from head-const to fully const. That should work
implicitly - which means using alias this. As far as I can tell, the only
reason that opDispatch comes into the picture here at all is because Proxy
was used in order to simplify Final's implementation. What Proxy is trying
to do with sub-typing is fundamentally opposite of what Final is trying to
do, since Proxy specifically does _not_ want implicit conversions, whereas
Final does want them. If Final is fixed so that it doesn't reuse Proxy, then
this whole problem goes away. Granted, that's a lot more work than what that
PR currently does, but it's the right solution unless we're going to drop
the idea of disallowing opDispatch and alias this on the same type, and
given how alias this and opDispatch work, it seems highly risky in general
to have them both be on the same type. It might actually work in some cases,
but it sure seems like it's begging for subtle bugs.

- Jonathan M Davis



More information about the Digitalmars-d mailing list