How to create compile-time container?

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Aug 31 20:48:14 UTC 2020


On Mon, Aug 31, 2020 at 08:39:10PM +0000, Andrey Zherikov via Digitalmars-d-learn wrote:
> On a low level, I want something like this code to work:
>     string[] arr;      // this can be any suitable type
> 
>     arr ~= "a";        // data is compile-time constant
> 
>     enum f = arr[0];   // fails with "Error: variable arr cannot be read at
> compile time"
>     enum b = arr[$-1]; // fails with "Error: variable arr cannot be read at
> compile time"
> 
> More generic description: I'm looking for a way to have a container
> (or any other type) that I can manipulate during compile time.
> Operations needed are similar to stack functionality: get first/last
> element, push_back and pop_back.
> 
> How can I do that?

First, read this article:

	https://wiki.dlang.org/User:Quickfur/Compile-time_vs._compile-time

Secondly, to answer your question: write regular ("runtime") code for
manipulating your data (also regular "runtime" data), and simply run the
function during CTFE. Almost 90% of the language is usable during CTFE,
so that's all you really need.


T

-- 
Маленькие детки - маленькие бедки.


More information about the Digitalmars-d-learn mailing list