Is `alias this` a mistake?

Kagamin spam at here.lot
Wed Aug 4 08:14:48 UTC 2021


On Tuesday, 3 August 2021 at 16:24:26 UTC, Paul Backus wrote:
> On Tuesday, 3 August 2021 at 15:52:07 UTC, NonNull 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
>
> `alias this` is typically used to
>
> - make a user-defined type behave like a subtype of some other 
> type
> - make a user-defined type implicitly convert to some other type
>
> ...but it does kind of a crappy job at both. It's very easy to 
> use `alias this` in ways that violate the [Liskov substitution 
> principle][1] (see for example [this thread][2]), so it fails 
> at being a good subtyping mechanism. And you can only have one 
> `alias this` per type, so it also fails at being a good 
> mechanism for user-defined implicit conversions (compare to 
> `opCast`, the mechanism for user-defined *explicit* conversion).
>
> [1]: https://en.wikipedia.org/wiki/Liskov_substitution_principle
> [2]: https://forum.dlang.org/thread/q6plhj$1l9$1@digitalmars.com

I use alias this for implicit conversion between reference and 
value type. It's not really subtyping and not really type 
conversion and works fine without humongous amounts of 
metaprogramming.


More information about the Digitalmars-d mailing list