Checking if CTFE is used?
MachineCode
jckj33 at gmail.com
Thu Dec 20 20:15:45 UTC 2018
On Tuesday, 18 December 2018 at 14:23:38 UTC, berni wrote:
> On Tuesday, 18 December 2018 at 13:53:01 UTC, Stefan Koch wrote:
>> Why would you need to know?
>
> Well, first out of curiosity. But there are also other reasons:
> I've got a large immutable array. Without CTFE I'd use some php
> script and add it as a large literal. With CTFE I don't need
> that php script nor do I need to put that large literal in the
> source code. But I need to know, that indeed the array is
> calculated by the compiler, else the solution with the php
> script would result in faster code.
>
How is the items in the array generated? if the function can be
called as following:
> enum myArr = generateArray();
then you know for sure you have a satic array of strings at
compile time.
I've used something similar, to generate a JSON string from all
UDAs in a give class, which I needed to pass to a C# application
(I made the D output a string from the request by command line
option). I did something like this:
string getAvailableForms()
{
import extensions.enumeration : staticMembers;
import asdf; // JSON serializer
import std.traits : getUDAs, hasUDA;
import externalPaths : ExternalAppPath, DisplayName;
import std.algorithm : startsWith;
import std.conv : to;
FormDisplayNameObj[] output;
static foreach(string fieldName; staticMembers!ExternalAppPath)
{{
static if(hasUDA!(__traits(getMember, ExternalAppPath,
fieldName), DisplayName))
{
if(fieldName.startsWith(startKey))
{
DisplayName dn = getUDAs!(__traits(getMember,
ExternalAppPath, fieldName), DisplayName)[0];
string id = extractDigits(fieldName[startKey.length - 1 ..
$]);
int i = to!int(id);
auto o = new FormDisplayNameObj();
o.value = dn.value;
o.index = i;
output ~= o;
}
}
}}
return output.serializeToJson;
}
then:
enum s = getAvailableForms;
writeln(s);
I did consider at first make a script to generate that string but
I managed to make it work with CTFE that way.
More information about the Digitalmars-d-learn
mailing list