CTFE Evaluation with JSON Objects

rbscott tjonsies818 at yahoo.com
Wed Jan 22 10:17:43 UTC 2020


I am running into an error during compilation and I can't figure 
out a way to work around it. The use case is pretty simple, to 
pull an integer out of a JSON Object at compile time.

import std.json;

void main()
     // Basic, should be the same principle?
     const jsonNumber = new JSONValue(1);
     pragma(msg, jsonNumber.integer);

     // Why does this work?
     const jsonObject = parseJSON(`{ "str" : "string", "integer": 
2 }`);
     pragma(msg, jsonObject["str"].str);

     // Why does this fail?
     pragma(msg, jsonObject["integer"].integer);

All of the compilers seem to fail with the error, "Error: 
reinterpretation through overlapped field integer is not allowed 
in CTFE". I think this is related to working with unions at 
compile time, but a. I am not sure why it doesn't happen when 
using a new JSONValue directly, and b. how to work around this 
limitation. Looking at the JSONValue source, the store is private 
and so even using a C cast wouldn't work without modifying the 

Any thoughts or ideas?


More information about the Digitalmars-d mailing list