alias this on module

via Digitalmars-d digitalmars-d at puremagic.com
Mon May 1 16:06:00 PDT 2017


On Monday, 1 May 2017 at 21:50:02 UTC, Jonathan Marler wrote:
> On Monday, 1 May 2017 at 12:41:19 UTC, Steven Schveighoffer 
> wrote:
>> On 4/30/17 7:59 PM, Jonathan Marler wrote:
>>> On Sunday, 30 April 2017 at 23:44:32 UTC, Steven 
>>> Schveighoffer wrote:
>>>> On 4/30/17 7:35 PM, Jonathan Marler wrote:
>>>>> Any reason why "alias this" doesn't work at the module 
>>>>> level?  If I
>>>>> recall correctly, a module is really just a "class" under 
>>>>> the hood, but
>>>>> when I tried to use it I got:
>>>>>
>>>>> Error: alias this can only be a member of aggregate, not 
>>>>> module
>>>>> <module-name>
>>>>>
>>>>
>>>> public import is to modules as alias this is to 
>>>> structs/classes :)
>>>>
>>>
>>> They're actually different.
>>>
>>> //
>>> // File: mymodule.d
>>> //
>>> module mymodule;
>>>
>>> struct Foo
>>> {
>>>     int bar;
>>>     void baz();
>>> }
>>> __gshared Foo foo;
>>> alias foo this; // using "alias this" on a module
>>
>> So you want to alias a struct to a module? That's different 
>> than what I thought, I thought you wanted to alias one 
>> module's members into another.
>>
>> I can't see a reason why it couldn't be added as a feature. I 
>> admit I'm not seeing the benefit though.
>>
>> You could simulate this via a mixin. e.g.:
>>
>> void baz()
>> {
>>    foo.baz;
>> }
>>
>> @property ref bar()
>> {
>>    return foo.bar;
>> }
>>
>> -Steve
>
> Ya now you get it. It's not a big deal, it's just that after I 
> thought about it, I couldn't think of a reason why it's not 
> supported.  I actually have an application for it too.  Since 
> modules are just classes under the hood, I was wondering if 
> implementing it would be as simple as commenting out an error 
> message like Andre's [nested import 
> example](https://www.youtube.com/watch?v=3NihZVcZqto&t=183s&#t=27m15s)

The common thing between modules and the other aggregate types 
(classes, interfaces, unions and structs) is that members of the 
former behave as if they were static members of the later. The 
difference, of course, is that since modules can have only static 
members, they can't be instantiated and by extension have no 
'this' pointer/reference. Because of this I think 'alias member 
this' on the module level would be nonsensical. Keep in mind that 
'alias member this' is a tool for establishing a subtyping 
relationship - i.e. 'this' instance can be used wherever 'member' 
can be used - and not just a way to do member access rewrite like 
opDispatch. There is no subtyping relationship between 
namespaces, which is what modules are effectively​.

On the other hand, 'alias bar = foo.bar' and module level static 
opDispatch seem like perfectly reasonable and desirable features.


More information about the Digitalmars-d mailing list