Compile-Time Only Function

Stefan Koch via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 23 03:52:25 PDT 2017


On Friday, 23 June 2017 at 10:46:11 UTC, Moinak Bhattacharyya 
wrote:
> Is there an annotation that declares a function compile-time 
> only? I'm attempting to use a mixin string generating function 
> that uses standard druntime imports, but I'm working in a 
> no-stdlib environment. I never need to use this function at 
> runtime (as is the case, I imagine, with a great many of mixin 
> string generating functions) so is there a way I can omit it?

There is no such annotation.
However what you can do is to put your function into a function 
literal and enclose it with if (__ctfe)

which would look like this

string GenString(bla, blubb)
{
   if (__ctfe) {
     auto functionBody = () {
      // .... your body with imports and all
     }
     return functionBody(bla, blubb);
   }
   else
   {
     assert(0);
   }
}

this should hopefully keep the compile-time code out of your 
executable.
It is even more effective if you use a immediately invoked 
function literal directly inside the mixin.
which will also cause the symbol for the function to invisible 
and therefore not generated.


More information about the Digitalmars-d mailing list