D import idiom compilation time

Ethan gooberman at gmail.com
Sat Jan 5 04:06:45 UTC 2019


On Saturday, 5 January 2019 at 01:58:15 UTC, Andrei Alexandrescu 
wrote:
> On 1/4/19 1:04 PM, Simen Kjærås wrote:
> [snip]
>
> So I guess we could define a short module called "std.autostd" 
> such that all uses of std.xxx would be resolved. Wow,

Looking at the above examples, and my own programming patterns 
lately. Every time I need something, I add it to my import 
statement, ie import std.algorithm : min, max, map, remove, sort;

Having each invocation import only that symbol defined at the end 
would be ideal. Something like:

struct from( string thismodule )
{
   template opDispatch( string symbol )
   {
       static if( __traits( compiles, { mixin( "import " ~ 
thismodule ~ ";" ); } )
                 && __traits( compiles, { mixin( "import " ~ 
thismodule ~ " : " ~ symbol ~ ";" ); } ) )
     {
       mixin( "import " ~ thismodule ~ " : " ~ symbol ~ ";");
       mixin( "alias opDispatch = " ~ symbol ~ ";" );
     }
     else
     {
       alias opDispatch = from!( thismodule ~ "." ~ symbol );
     }
   }
}

int main( string[] args )
{
     alias std = from!"std";

     std.stdio.writeln( "It works!" );
     return 0;
}



More information about the Digitalmars-d mailing list