Hello,
I am currently making a wrapper around libplist in D, and to get a good syntax I first thought about making a pl
function with bunch of overloads allowing to convert strings, integers, booleans into the corresponding Plist
type. It is working but the problem here is that associative array literals are not preserving order (they are immediately hashed, and so all the keys are in a random order).
To overcome that, I create a template plistDict
as such:
template plistDict(alias U) {
PlistDict pl() {
auto dict = new PlistDict();
static foreach (elem; dict) {
dict[elem.key] = elem.value;
}
return dict;
}
}
The static foreach
ensures everything is running at compile time, and so no hash should mess with the keys. But the problem here is that it tries to evaluate at compile time the value of each entry in the associative array (what it can't do since it's a wrapper around a C library!).
The same kind of issue can be encountered in this small code snippet:
string crashingFunction() {
assert(!__ctfe);
return "OK";
}
template macroLikeTemplate(alias U) {
string macroLikeTemplate() {
return "" ~ U;
}
}
void main()
{
macroLikeTemplate!(crashingFunction());
}
Here, the function is evaluated before the element is passed to the template, and is failing (here because of an assert, but we can imagine something that would fail only at compile-time like a C call), while the substitution could just work fine if it wasn't evaluated before hand.
Is there any way to make this work?