Compile-Time Only Function
Mike via Digitalmars-d
digitalmars-d at puremagic.com
Fri Jun 23 05:23:23 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?
I don't know of any way to generate a compile-time error if a
function cannot be evaluated at compile-time. I think you will
have to use a manifest constant
(https://dlang.org/spec/enum.html#manifest_constants) to force
evaluation at compile time.
// If you're not using druntime, you'll have to define the
// string type as an alias to an immutable char array.
// See
https://github.com/dlang/druntime/blob/master/src/object.d#L41
alias immutable(char)[] string;
string GenString(string a, string b)
{
return a ~ ", " ~ b;
}
void main()
{
// `result` is a manifest constant that contains your
// compile-time string, do as you wish with it
enum result = GenString("Hello", "World");
}
More information about the Digitalmars-d
mailing list