Named multi-imports

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Fri Aug 18 01:53:58 PDT 2017


On 18.08.2017 01:25, jmh530 wrote:
> On Thursday, 17 August 2017 at 21:49:38 UTC, Timon Gehr wrote:
>>
>> private struct oo{
>>     import std.stdio: writeln, write;
>>     import std.algorithm: filter, map;
>>     // …
>> }
>>
>> void main(){
>>     oo.write("result: ");
>> oo.writeln(oo.map!(x=>x/2)(oo.filter!(x=>x%2==0)([1,2,3,4,5,6,10])));
>> }
> 
> Would not have thought to do that! Very cool.
> 
> Quick follow up: is there a reason this why renamed selective imports 
> don't work this way?

I don't think there is. (I.e. I think it is indeed a bug.)

> As in the bug from the link below we discussed above
> 
> https://issues.dlang.org/show_bug.cgi?id=17756
> 
> Re-writing the import like this seems like the perfect bug fix?

It's one way to do it, but the compiler does not necessarily need to 
generate a new type. Note that unfortunately, renamed imports don't 
overload, so this would not work even with the fixed bug:

import oo=std.stdio: writeln, write;
import oo=std.algorithm: filter, map; // error: oo redefined

void main(){
     oo.write("result: ");
     oo.writeln(oo.map!(x=>x/2)(oo.filter!(x=>x%2==0)([1,2,3,4,5,6,10])));
}

I think this is working as designed, but IMO they should just overload 
(using distinct overload sets so the semantics is similar to the case 
when using the struct), as should named mixins.


More information about the Digitalmars-d mailing list