Jump to page: 1 2 3
Thread overview
Is `alias this` a mistake?
Aug 03, 2021
NonNull
Aug 03, 2021
H. S. Teoh
Aug 03, 2021
Paul Backus
Aug 04, 2021
Kagamin
Aug 03, 2021
IGotD-
Aug 03, 2021
Tejas
Aug 03, 2021
bachmeier
Aug 03, 2021
Alexandru Ermicioi
Aug 04, 2021
bachmeier
Aug 04, 2021
jmh530
Aug 04, 2021
H. S. Teoh
Aug 05, 2021
jmh530
Aug 05, 2021
H. S. Teoh
Aug 05, 2021
IGotD-
Aug 05, 2021
H. S. Teoh
Aug 06, 2021
wjoe
Aug 06, 2021
Paul Backus
Aug 06, 2021
H. S. Teoh
Aug 07, 2021
wjoe
Aug 07, 2021
tsbockman
Aug 07, 2021
IGotD-
Aug 05, 2021
jmh530
Aug 07, 2021
12345swordy
Aug 05, 2021
IGotD-
Aug 04, 2021
Mike
Aug 04, 2021
Nick Treleaven
Aug 04, 2021
Guillaume Piolat
Aug 05, 2021
TheGag96
August 03, 2021

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

August 03, 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
August 03, 2021

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 (see for example this thread), 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).

August 03, 2021

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

Usually inheritance can be replaced with composition and alias this has been the way to do that. Instad of alias this, Walter wants us to use mixin templates for composition. I have no problems with this but the documentation and marketing has totally failed regarding this few seem to know about mixin templates its usage and limitations. The D community needs to be market mixin templates better.

August 03, 2021

On Tuesday, 3 August 2021 at 16:34:35 UTC, IGotD- 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

Usually inheritance can be replaced with composition and alias this has been the way to do that. Instad of alias this, Walter wants us to use mixin templates for composition. I have no problems with this but the documentation and marketing has totally failed regarding this few seem to know about mixin templates its usage and limitations. The D community needs to be market mixin templates better.

This is the first time I'm hearing about this application of mixin templates.
I've seen Adam also say that MTs are underrated but I just don't understand how you can say that they replace inheritance.

Think its possible for someone to write an article on the D blog? I'm interested in seeing this.

August 03, 2021

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

The only argument I've heard against alias this is that it can be misused. That's kind of a joke argument in a language that has pointers.

August 03, 2021

On Tuesday, 3 August 2021 at 17:18:48 UTC, bachmeier 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

The only argument I've heard against alias this is that it can be misused. That's kind of a joke argument in a language that has pointers.

Fyi, 'alias this' nicely fits for doing auto unboxing of a wrapper type into wrapped type, similar to how java does unboxing of primitives, not sure that it will be possible with mixin templates.

August 04, 2021

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 don't have much value for classes, as the same can be achieved with interfaces, inheritance, and D's killer metaprogramming facilities.

Where alias this does have value is with structs.

My suggestion has been to deprecate (or at least discourage) alias this for classes, but invest more in alias this for structs (e.g. add multiple alias this features for structs).

More on that can be read at https://forum.dlang.org/post/vggskphkqxtriqnavmnf@forum.dlang.org

August 04, 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 (see for example this thread), 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).

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.

August 04, 2021

On Wednesday, 4 August 2021 at 00:29:21 UTC, Mike wrote:

>

My suggestion has been to deprecate (or at least discourage) alias this for classes,

It might be useful to allow alias this in a final class for when you want a wrapper that is a reference type.

>

More on that can be read at https://forum.dlang.org/post/vggskphkqxtriqnavmnf@forum.dlang.org

Thanks.

« First   ‹ Prev
1 2 3