Is `alias this` a mistake?

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Aug 3 16:15:00 UTC 2021


On Tue, Aug 03, 2021 at 03:52:07PM +0000, NonNull via Digitalmars-d wrote:
> Here, further down, I just saw Walter indicate his opinion that `alias
> this` is a mistake. Any thoughts?
> 
> https://news.ycombinator.com/item?id=28029184

This has been discussed many times before.  Walter's stance is that it's
a mistake because it's essentially another way of implementing
subtyping, that has unclear interactions with well-established subtyping
mechanisms (OO-style inheritance, in particular).  This leads to a
cascade of special cases that's hard to understand and adds a lot of
complexity to the language for only meager benefits.

A secondary reason is that it allows implicit conversions, and Walter
has always had a low opinion of implicit conversions, especially the
user-defined kind.

I used to be a big fan of `alias this`, but after some actual experience
with using it in my own code, I've come to agree with Walter more and
more that it was a mistake.  It's cool for quick hacks to make things
work with code designed to receive a different type (and sometimes I
still can't resist the temptation to use it), but in the long run it
hurts code readability and maintainability.  I've come to realize that
whenever something in my code demands `alias this` as the answer, it's
almost always a symptom of poor code design on my part.  My code has
often benefitted a lot from being rewritten to *not* need `alias this`.


T

-- 
He who sacrifices functionality for ease of use, loses both and deserves neither. -- Slashdotter


More information about the Digitalmars-d mailing list