D import idiom compilation time

Dgame r.schuett.1987 at gmail.com
Sat Jan 5 21:14:37 UTC 2019


On Saturday, 5 January 2019 at 17:30:15 UTC, Neia Neutuladh wrote:
> On Sat, 05 Jan 2019 06:52:14 -0800, H. S. Teoh wrote:
>> Hmm. Couldn't you make it work by making it lazy? I.e., have 
>> opDispatch return a struct that encapsulates a partial package 
>> path, and that contains a further opDispatch to resolve the 
>> next component in the qualified name, and that only resolves 
>> to the actual symbol when it actually references an actual 
>> symbol?
>
> Right! However, you can't tell the difference between accessing 
> a symbol that doesn't exist and accessing a package.
>
> You'll see a difference in things like:
>
>     static if (is(typeof(from.std.socket.Sockte)))
>
> In this lazy model, that's going to be a 
> FromImpl!"std.socket.Sockte" and will pass. In the eager model, 
> it would be std.socket.Sockte, which doesn't exist.

I'm curious about that. Would that work or did I miss something?

----
template isSymbolInModule(string module_, string symbol)
{
     static if (__traits(compiles, { mixin("import ", module_, 
";"); })) {
         enum import_ = module_ ~ ":" ~ symbol;
         enum isSymbolInModule = __traits(compiles, { 
mixin("import ", import_, ";"); });
     } else {
         enum isSymbolInModule = false;
     }
}

struct FromImpl(string module_)
{
     template opDispatch(string symbol)
     {
         static if (isSymbolInModule!(module_, symbol)) {
             mixin("import ", module_, "; alias opDispatch = ", 
symbol, ";");
         } else {
             enum import_ = module_.length == 0 ? symbol : module_ 
~ "." ~ symbol;
             enum opDispatch = FromImpl!(import_)();
         }
     }
}

from.std.stdio.writeln("Hallo");
auto _ = from.std.datetime.stopwatch.AutoStart.yes;
----


More information about the Digitalmars-d mailing list