D import idiom compilation time

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Jan 5 16:42:19 UTC 2019


On 1/4/19 11:13 PM, H. S. Teoh wrote:
> On Fri, Jan 04, 2019 at 08:54:44PM -0500, Andrei Alexandrescu via Digitalmars-d wrote:
>> On 1/4/19 8:11 AM, bauss wrote:
>>> On Thursday, 3 January 2019 at 23:54:42 UTC, SrMordred wrote:
>>>> struct _std
>>>> {
>>>>    template opDispatch(string moduleName)
>>>>    {
>>>>      mixin("import opDispatch = std." ~ moduleName ~ ";");
>>>>    }
>>>> }
>>>
>>>
>>> There is nothing that's actually stopping you from just calling it
>>> std.
>>>
>>> It will work just as fine.
>>>
>>> That way you can end up with
>>>
>>> std.stdio.writeln("...");
>>>
>>> Instead of:
>>> _std.stdio.writeln("...");
>>
>> This is quite amazing.
> 
> It doesn't have to be tied to a specific root package. Just call it
> "autoimport", or something short like "from":
> 
> 	struct from
> 	{
> 	  template opDispatch(string moduleName)
> 	  {
> 	    mixin("import opDispatch = " ~ moduleName ~ ";");
> 	  }
> 	}
> 
> Then just write:
> 
> 	from.std.stdio.writeln("...");
> 	from.some.other.package.func(1, 2, 3);
> 
> and so on.
> 
> 
> T
> 

Cool. Here's the previous clunkier implementation:

https://github.com/dlang/druntime/pull/1756

And here's the bug preventing it:

https://issues.dlang.org/show_bug.cgi?id=17181

Is the bug also affecting this newly invented idiom?


Andrei


More information about the Digitalmars-d mailing list