www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Lazy private selective imports

reply "Peter Alexander" <peter.alexander.au gmail.com> writes:
Would it be possible to perform private selective imports lazily? 
i.e. only import when the symbol selectively imported is 
requested.

--------------------------------------

// not used => std.stdio not imported
import std.stdio : writeln;
void main()

--------------------------------------

// used => std.stdio imported
import std.stdio : writeln;
void main() { writeln("Hello, world"); }

--------------------------------------

// non-selective std.stdio imported
import std.stdio;
void writeln(
void main() {}

--------------------------------------

Are there problems with this?

One semantic difference this would make is that module 
constructors wouldn't be run in the first case whereas they would 
be without this change. Public selective imports would need to be 
done eagerly.

The idea is to reduce code bloat and parse times without having 
to resort to local imports everywhere and hacks like this: 
https://github.com/D-Programming-Language/phobos/pull/2047/files
Mar 24 2014
next sibling parent "Peter Alexander" <peter.alexander.au gmail.com> writes:
On Monday, 24 March 2014 at 21:18:49 UTC, Peter Alexander wrote:
 // non-selective std.stdio imported
 import std.stdio;
 void writeln(
 void main() {}
Oops, that unfinished line isn't meant to be there... :-)
Mar 24 2014
prev sibling next sibling parent reply "Andrej Mitrovic" <andrej.mitrovich gmail.com> writes:
On Monday, 24 March 2014 at 21:18:49 UTC, Peter Alexander wrote:
 Would it be possible to perform private selective imports 
 lazily?
This was discussed fairly recently: http://forum.dlang.org/thread/l8t2o1$kq0$1 digitalmars.com?page=9 (and previous or next pages)
Mar 24 2014
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Monday, 24 March 2014 at 21:21:30 UTC, Andrej Mitrovic wrote:
 On Monday, 24 March 2014 at 21:18:49 UTC, Peter Alexander wrote:
 Would it be possible to perform private selective imports 
 lazily?
This was discussed fairly recently: http://forum.dlang.org/thread/l8t2o1$kq0$1 digitalmars.com?page=9 (and previous or next pages)
This is a very recent issue, as a matter of fact, I ran into it *TODAY*: https://github.com/D-Programming-Language/phobos/pull/2047 By combining templates and aliases, you can have a library solution: //---- template lazyWriteln() { import std.stdio : writeln; alias lazyWriteln = std.stdio.writeln; } void main() { lazyWriteln(5); } //---- Heck, with proper renamed imports, you can even re-use the initial name: //---- template writeln() { import std.stdio : stdwriteln = writeln; alias writeln = stdwriteln; } void main() { writeln(5); } //---- HOWEVER (word of warning), for the second solutions, I've run into conflicting alias issues: std/conv.d(4864): Error: std.string.format!(char, uint, string, uint).format at std/string.d(2401) conflicts with std.string.format!(char, uint, string, uint).format at std/string.d(2401) I didn't dig very far to know if this is 313/314 related, or a new issue though. In any case, while not as convenient as a built-in "lazy import", it could be a solution worth digging into.
Mar 24 2014
parent reply "Peter Alexander" <peter.alexander.au gmail.com> writes:
On Monday, 24 March 2014 at 21:43:36 UTC, monarch_dodra wrote:
 On Monday, 24 March 2014 at 21:21:30 UTC, Andrej Mitrovic wrote:
 On Monday, 24 March 2014 at 21:18:49 UTC, Peter Alexander 
 wrote:
 Would it be possible to perform private selective imports 
 lazily?
This was discussed fairly recently: http://forum.dlang.org/thread/l8t2o1$kq0$1 digitalmars.com?page=9 (and previous or next pages)
This is a very recent issue, as a matter of fact, I ran into it *TODAY*: https://github.com/D-Programming-Language/phobos/pull/2047
See the link at the end of the OP ;-)
 By combining templates and aliases, you can have a library 
 solution:

 ...

 In any case, while not as convenient as a built-in "lazy 
 import", it could be a solution worth digging into.
This is why I brought this up. You have provided a library solution, but it's ugly to have to do that manually for every function.
Mar 24 2014
parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Monday, 24 March 2014 at 22:06:07 UTC, Peter Alexander wrote:
 On Monday, 24 March 2014 at 21:43:36 UTC, monarch_dodra wrote:
 In any case, while not as convenient as a built-in "lazy 
 import", it could be a solution worth digging into.
This is why I brought this up.
Oops. I *thought* the coincidence was uncanny :)
 You have provided a library solution, but it's ugly to have to 
 do that manually for every function.
Yeah. It's ugly. It also fails if said alias needs parameters (eg: "format!char"). It would be nice if selective and/or static imports were lazy.
Mar 24 2014
prev sibling parent reply Martin Nowak <code dawg.eu> writes:
On 03/24/2014 10:18 PM, Peter Alexander wrote:
 Would it be possible to perform private selective imports lazily? i.e.
 only import when the symbol selectively imported is requested.
Laziness only makes sense for static imports, because otherwise the compiler needs to check for overloads during symbol lookup.
Mar 25 2014
next sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 3/25/14, 3:44 PM, Martin Nowak wrote:
 On 03/24/2014 10:18 PM, Peter Alexander wrote:
 Would it be possible to perform private selective imports lazily? i.e.
 only import when the symbol selectively imported is requested.
Laziness only makes sense for static imports, because otherwise the compiler needs to check for overloads during symbol lookup.
I think it makes sense for all imports because those in turn have other nonpublic imports. -- Andrei
Mar 25 2014
prev sibling parent reply "Peter Alexander" <peter.alexander.au gmail.com> writes:
On Tuesday, 25 March 2014 at 22:44:30 UTC, Martin Nowak wrote:
 On 03/24/2014 10:18 PM, Peter Alexander wrote:
 Would it be possible to perform private selective imports 
 lazily? i.e.
 only import when the symbol selectively imported is requested.
Laziness only makes sense for static imports, because otherwise the compiler needs to check for overloads during symbol lookup.
If I only import writeln from std.stdio then you only overload resolution that could effect is that of the symbol writeln. That means you only need to process the import when writeln is encountered. No? e.g. import std.stdio : writeln; void main() { foo(); } There should be no need to look in std.stdio at all here, regardless of whether it contains foo or not because I'm only importing writeln.
Mar 25 2014
parent "Martin Nowak" <code dawg.eu> writes:
On Tuesday, 25 March 2014 at 23:27:22 UTC, Peter Alexander wrote:
 There should be no need to look in std.stdio at all here, 
 regardless of whether it contains foo or not because I'm only 
 importing writeln.
True that, I mixed up the discussion with another thread. I even mentioned the same here (http://forum.dlang.org/post/lad0fs$glp$1 digitalmars.com).
Aug 04 2014