How to create compile-time container?

Steven Schveighoffer schveiguy at gmail.com
Tue Sep 1 18:57:30 UTC 2020


On 9/1/20 2:19 PM, Andrey Zherikov wrote:
> On Monday, 31 August 2020 at 20:44:16 UTC, Adam D. Ruppe wrote:
>> On Monday, 31 August 2020 at 20:39:10 UTC, Andrey Zherikov wrote:
>>> How can I do that?
>>
>> You can use a normal string[] BUT it is only allowed to be modified 
>> inside its own function.
>>
>> Then you assign that function to an enum or whatever.
>>
>>
>> string[] ctGenerate() {
>>    string[] list;
>>    list ~= "stuff";
>>    return list;
>> }
>>
>> enum list = ctGenerate();
>>
>>
>> That's all allowed. But CTFE is now allowed to read or modify anything 
>> outside its own function; you can't have two separate function calls 
>> build up a shared list (unless you can somehow call them both together 
>> like `enum list = ctGenerate() ~ other_thing();`
> 
> 
> The thing I'm trying to implement is: I have a function foo(string s)() 
> and some "state"; this function should override this "state" (using "s" 
> param) for all code within this function (note that code can execute 
> other modules that can refer to the same "state"). The problem is that I 
> need this overridden "state" to be compile-time constant to be used in 
> mixin. Any ideas how I can do this?

string overrideState(string s)
{
    // work your magic here, it's normal D code!
}

void foo(string s)()
{
    mixin(overrideState(s));
}

-Steve


More information about the Digitalmars-d-learn mailing list