new discovery: import only if available

Adam D. Ruppe destructionator at gmail.com
Fri Jun 21 05:41:29 PDT 2013


OMG guys this finishes the druntime extension problem!


Try it yourself, open up dmd2/src/druntime/import/object.di and 
find template RTInfo.

Change it to this:

template RTInfo(T)
{
     static if(__traits(compiles, { import druntime.extensions; 
auto a =  druntime.extensions.    RTInfo!T; })) {
         import druntime.extensions;
          enum RTInfo = druntime.extensions.RTInfo!T;
     } else
         enum RTInfo = cast(void*)0x12345678;
}



Make a test program with just "struct Test {} "

And a helper file with contents:

===
module druntime.extensions;

template RTInfo(T) {
         static if(T.stringof == "Test") {
                 pragma(msg, "Here!");
         }

         enum RTInfo = null;
}
==



Compile without extensions:

$ dmd test23 -main
$


No messages, no errors, just as it should be.


Now add our helper file to the command line....


$ dmd test23 -main dext.d
Here!
$


It triggered! Boom, we have the potential for *project-wide* 
modifications to RTInfo without modifying druntime. Combine this 
with the other techniques I've talked about for rtinfo and we 
have it all.

You wouldn't necessarily have to add it specifically to the 
command line btw, you could put it in a file called 
druntime/extensions.d in your import directory as well, 
especially since it is a template.


More information about the Digitalmars-d mailing list