A New Import Idiom`

Johan Engelen via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Mon Feb 13 11:41:11 PST 2017


On Monday, 13 February 2017 at 15:04:53 UTC, Daniel N wrote:
> On Monday, 13 February 2017 at 15:00:24 UTC, jmh530 wrote:
>>
>> With the Jack Stouffer comparison, wouldn't it be fairer to do:
>>
>> import std.datetime : Systime;
>> import std.traits : isIntegral;
>
> Probably, please help measuring your idea and post it here. I 
> intentionally chose a test made by someone else to avoid 
> biasing from my part. It was the only test I found at time of 
> writing.

In case you don't actually use the function, things are faster 
and you get a smaller object file because the template is never 
instantiated. So if the function is never used in the module (it 
is just there for other modules to use), then things are indeed 
faster and smaller. But you also don't get compile checking on 
it; putting only
```
void func(T)(from!"std.datetime".SysTime a, T value)
     if (from!"std.traits".isIntegral!T)
{
     import std.stdio : writeln;
     writeln(a, value);
}
```
in a file compiles fast without complaints about an undefined 
`from`.

But for a full program, compile time is the same (it's really 
short so you can't really conclude much from it), and binary size 
as well (LDC 1.1.0, -O3). For my test cases below, the binaries 
are identical, except for around 30 bytes...

My test cases:
```
template from(string moduleName)
{
   mixin("import from = " ~ moduleName ~ ";");
}

void func(T)(from!"std.datetime".SysTime a, T value)
     if (from!"std.traits".isIntegral!T)
{
     import std.stdio : writeln;
     writeln(a, value);
}

void main()
{
     func!int(from!"std.datetime".SysTime(), 1);
}
```

```
import std.datetime;
import std.traits;

void func(T)(SysTime a, T value) if (isIntegral!T)
{
     import std.stdio : writeln;
     writeln(a, value);
}

void main()
{
     import std.datetime : SysTime;
     func!int(SysTime(), 1);
}
```

-Johan


More information about the Digitalmars-d-announce mailing list