Jump to page: 1 25  
Page
Thread overview
January 03
There is a veredict about the compilation speedup of the "New Import Idiom"?

Currently i´m using it this way:

struct _std
{
  template opDispatch(string moduleName)
  {
    mixin("import opDispatch = std." ~ moduleName ~ ";");
  }
}
...
//if used single time on scope.
_std.algorithm.max(a,b);

My question is that, in the long run, this will be worth the compilation time gains, or is just negligible and I should just import the normal way.
(and keep the code more sane)

Thanks!
January 04
On Thursday, 3 January 2019 at 23:54:42 UTC, SrMordred wrote:
> There is a veredict about the compilation speedup of the "New Import Idiom"?
>
> Currently i´m using it this way:
>
> struct _std
> {
>   template opDispatch(string moduleName)
>   {
>     mixin("import opDispatch = std." ~ moduleName ~ ";");
>   }
> }
> ...
> //if used single time on scope.
> _std.algorithm.max(a,b);
>
> My question is that, in the long run, this will be worth the compilation time gains, or is just negligible and I should just import the normal way.
> (and keep the code more sane)
>
> Thanks!


Yes.


--- testPerf.d

struct fromStd
{
    template opDispatch(string moduleName)
    {
        mixin("import opDispatch = std." ~ moduleName ~ ";");
    }
}

version (UseFrom)
{
    auto foo(T)(T arg) if (fromStd.typecons.isBitFlagEnum!T)
    {
        return arg;
    }
}
else
{
    import std.typecons : isBitFlagEnum;
    auto foo(T)(T arg) if (isBitFlagEnum!T)
    {
        return arg;
    }
}



$ time dmd -c testperf.d

real	0m0.050s

$ time dmd -c -version=UseFrom testperf.d

real	0m0.016s




January 04
On Friday, 4 January 2019 at 02:43:01 UTC, Jonathan Marler wrote:
> On Thursday, 3 January 2019 at 23:54:42 UTC, SrMordred wrote:
>> There is a veredict about the compilation speedup of the "New Import Idiom"?
>>
>> Currently i´m using it this way:
>>
>> struct _std
>> {
>>   template opDispatch(string moduleName)
>>   {
>>     mixin("import opDispatch = std." ~ moduleName ~ ";");
>>   }
>> }
>> ...
>> //if used single time on scope.
>> _std.algorithm.max(a,b);
>>
>> My question is that, in the long run, this will be worth the compilation time gains, or is just negligible and I should just import the normal way.
>> (and keep the code more sane)
>>
>> Thanks!
>
>
> Yes.
>
>
> --- testPerf.d
>
> struct fromStd
> {
>     template opDispatch(string moduleName)
>     {
>         mixin("import opDispatch = std." ~ moduleName ~ ";");
>     }
> }
>
> version (UseFrom)
> {
>     auto foo(T)(T arg) if (fromStd.typecons.isBitFlagEnum!T)
>     {
>         return arg;
>     }
> }
> else
> {
>     import std.typecons : isBitFlagEnum;
>     auto foo(T)(T arg) if (isBitFlagEnum!T)
>     {
>         return arg;
>     }
> }
>
>
>
> $ time dmd -c testperf.d
>
> real	0m0.050s
>
> $ time dmd -c -version=UseFrom testperf.d
>
> real	0m0.016s

I should clarify.  It depends what type of code you are writing.  If you are writing library code in which only small parts of modules may be used then importing this way will allow apps to use your library while only importing the modules they need.

If you are writing program code where you don't need to expose subsets of code to other modules, you just want to use library code then in most cases there's no benefit to lazily importing other modules since you're always going to need them.

January 04
On Thursday, 3 January 2019 at 23:54:42 UTC, SrMordred wrote:
> There is a veredict about the compilation speedup of the "New Import Idiom"?
>
> Currently i´m using it this way:
>
> struct _std
> {
>   template opDispatch(string moduleName)
>   {
>     mixin("import opDispatch = std." ~ moduleName ~ ";");
>   }
> }
> ...
> //if used single time on scope.
> _std.algorithm.max(a,b);
>
> My question is that, in the long run, this will be worth the compilation time gains, or is just negligible and I should just import the normal way.
> (and keep the code more sane)
>
> Thanks!

I like this idiom way more than the other nasty from!"..." syntax. Though you need to define a struct for any library you'd want to use it with, not that big of a deal. Thanks for sharing.
January 04
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("...");
January 04
On Friday, 4 January 2019 at 13:07:14 UTC, Rubn wrote:
> On Thursday, 3 January 2019 at 23:54:42 UTC, SrMordred wrote:
>> There is a veredict about the compilation speedup of the "New Import Idiom"?
>>
>> Currently i´m using it this way:
>>
>> struct _std
>> {
>>   template opDispatch(string moduleName)
>>   {
>>     mixin("import opDispatch = std." ~ moduleName ~ ";");
>>   }
>> }
>> ...
>> //if used single time on scope.
>> _std.algorithm.max(a,b);
>>
>> My question is that, in the long run, this will be worth the compilation time gains, or is just negligible and I should just import the normal way.
>> (and keep the code more sane)
>>
>> Thanks!
>
> I like this idiom way more than the other nasty from!"..." syntax. Though you need to define a struct for any library you'd want to use it with, not that big of a deal. Thanks for sharing.

The struct could of course be templated:

struct from(string namespace) {
    template opDispatch(string subnamespace) {
        mixin("import opDispatch = "~namespace~"."~subnamespace~";");
    }
}

unittest {
    alias std = from!"std";
    std.stdio.writeln("ohai");
}

And slightly more fancy, for those pesky nested packages:


struct from(string namespace) {
    static if (__traits(compiles, { mixin("import "~namespace~";"); })) {
        mixin("import __from = "~namespace~";");
    }
    template opDispatch(string subnamespace) {
        static if (__traits(compiles, { mixin("import "~namespace~"."~subnamespace~";"); })) {
            alias opDispatch = .from!(namespace~"."~subnamespace);
        } else {
            mixin("alias opDispatch = __from."~subnamespace~";");
        }
    }
}

unittest {
    alias std = from!"std";

    std.stdio.writeln("ohai");

    // This would be impossible with the first version:
    static assert(std.range.primitives.isInputRange!(int[]));

    // As would single-level imports:
    alias myLibrary = from!"myLibrary";
    myLibrary.someFunction();
}

--
  Simen
January 04
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.
January 04
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,
January 05
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,

+1, awesome idea.

 - Elias
January 05
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;
}

« First   ‹ Prev
1 2 3 4 5