digitalmars.D - Lazy private selective imports
- Peter Alexander (25/25) Mar 24 2014 Would it be possible to perform private selective imports lazily?
- Peter Alexander (2/6) Mar 24 2014 Oops, that unfinished line isn't meant to be there... :-)
- Andrej Mitrovic (4/6) Mar 24 2014 This was discussed fairly recently:
- monarch_dodra (42/48) Mar 24 2014 This is a very recent issue, as a matter of fact, I ran into it
- Peter Alexander (5/22) Mar 24 2014 This is why I brought this up. You have provided a library
- monarch_dodra (6/12) Mar 24 2014 Oops.
- Martin Nowak (3/5) Mar 25 2014 Laziness only makes sense for static imports, because otherwise the
- Andrei Alexandrescu (3/9) Mar 25 2014 I think it makes sense for all imports because those in turn have other
- Peter Alexander (11/18) Mar 25 2014 If I only import writeln from std.stdio then you only overload
- Martin Nowak (4/7) Aug 04 2014 True that, I mixed up the discussion with another thread.
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
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
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
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: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.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
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:See the link at the end of the OP ;-)On Monday, 24 March 2014 at 21:18:49 UTC, Peter Alexander wrote:This is a very recent issue, as a matter of fact, I ran into it *TODAY*: https://github.com/D-Programming-Language/phobos/pull/2047Would 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)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
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:Oops. I *thought* the coincidence was uncanny :)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.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
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
On 3/25/14, 3:44 PM, Martin Nowak wrote:On 03/24/2014 10:18 PM, Peter Alexander wrote:I think it makes sense for all imports because those in turn have other nonpublic imports. -- AndreiWould 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
On Tuesday, 25 March 2014 at 22:44:30 UTC, Martin Nowak wrote:On 03/24/2014 10:18 PM, Peter Alexander wrote: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.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
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