Manipulating variables without evaluating them

Dadoum dadoum at protonmail.com
Mon May 8 11:42:44 UTC 2023


On Monday, 8 May 2023 at 11:06:59 UTC, Salih Dincer wrote:
> On Sunday, 7 May 2023 at 17:11:52 UTC, Dadoum wrote:
>> 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).
>
> It's hard to figure out what you want but there are errors in 
> your code. This works:
>
> ```d
> auto plistDict(alias AA)()
> {
>   auto dict = new string[AA.length];
>   foreach (key, value; AA)
>   {
>     dict[key] = value;
>   }
>   return dict;
> }
>
> import std.stdio;
>
> void main()
> {
>   auto plist = plistDict!([0: "zero",
>                            1: "one",
>                            2: "two"]);
>   plist.writeln; // ["zero", "one", "two"]
> }
> ```
>
> And yes, the data is not converted at compile time.
>
> SDB at 79

I should have worded my question differently. The problem here is 
that the values are not constant at compile time. It should be 
emulated with

```d
auto plistDict(alias AA)()
{
	auto dict = new string[AA.length];
	foreach (key, value; AA)
	{
		dict[key] = value;
	}
	return dict;
}

import std.stdio;

extern(C) void functionThatCannotBeEvaluatedAtCompileTime();

string conversionFunction(string val) {
     functionThatCannotBeEvaluatedAtCompileTime();
     return val;
}

void main()
{
	auto plist = plistDict!([
		0: "zero".conversionFunction, // Error: 
`functionThatCannotBeEvaluatedAtCompileTime` cannot be 
interpreted at compile time, because it has no available source 
code
		1: "one".conversionFunction,
		2: "two".conversionFunction,
	]);
	plist.writeln;
}
```

Here, it complains that the values cannot be evaluated since it 
has no source code. But I don't care about the source code here, 
I just want the code on the left to be executed at runtime, not 
evaluate it right at compilation.

(and yeah in my first snippet I shouldn't iterate through the 
runtime dict but the compile time U, I made a mistake while 
compacting the code to post it here).

Is there a way to use the associative array here at compile time 
while avoiding the evaluation of those keys?


More information about the Digitalmars-d mailing list