What would be the consequence of implementing interfaces as fat pointers ?

Manu turkeyman at gmail.com
Tue Apr 1 00:11:51 PDT 2014


On 1 April 2014 15:53, Walter Bright <newshound2 at digitalmars.com> wrote:

> On 3/31/2014 10:02 PM, Manu wrote:
>
>> It's a really common pattern, it's obviously very useful, but it's
>> surprising to
>> me that people think that it's like, 'cool'.
>> I get why it's done, and it's cool that D can do this (I use it a lot in
>> my
>> code), but I don't feel it's particularly elegant.
>>
>
> 'alias this' is inelegant (sorry Andrei) but it was designed for precisely
> this purpose - being able to use a struct to wrap any other type, and
> forward to and override behaviors of that type. Nobody has found a better
> way.
>

I don't think this is the only instance where alias this is useful though.
It's one of those features that's unintuitive at first, but I find it pops
up surprisingly often.
This is a super common pattern however, and my point was, that language
already has a well defined reference type that is convenient and familiar,
but it comes with some baggage that's not always desired.

It's kind of irrelevant though; the leading point was that one advantage
might be that carrying the vtable beside the instance pointer would
eliminate the hidden fiends in the class instance, but there's the
classinfo pointer too.
Incidentally, why did you go with a dedicated classinfo pointer rather than
use the 1st slot of the vtable like c++?

Fortunately, the inelegance can be encapsulated within that type, and the
> user of the type need not be even aware of it.
>

Sure, but my point was that it seems to be _really_ frequently occurring.
It's relatively unprecedented in D to be happy with such a commitment to
boilerplate like that.
I can imagine there's a strong temptation to just reach for a class even
though it's not appropriate in all situations. It's a lot less cognitive
load; inexperienced programmers won't have trouble with the boilerplate
implementation, or what to do when they hit edge cases.

Remember my halffloat implementation? It relied on 'alias this' to work.
> Just try doing that in C++ <g>.
>

Of course, I use alias this all the time too for various stuff. I said
before, it's a useful tool and it's great D *can* do this stuff, but I'm
talking about this particular super common use case where it's used to hack
together nothing more than a class without a vtable, ie, a basic ref type.
I'd say that's worth serious consideration as a 1st-class concept?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20140401/e761e6c3/attachment.html>


More information about the Digitalmars-d mailing list