digitalmars.D.learn - Compile-time associative array
- boolangery (19/19) Mar 19 2019 Hi,
- Bastiaan Veelo (11/30) Mar 19 2019 Yes, I think so. If you would use the enum AA multiple times, it
- Steven Schveighoffer (8/10) Mar 19 2019 Yes, this is the issue -- the runtime AA depends on druntime, which is
- boolangery (21/31) Mar 20 2019 Got it ! Thank you, so I need to write:
- Bastiaan Veelo (2/21) Mar 20 2019 Yes, that looks correct to me.
- Steven Schveighoffer (4/37) Mar 20 2019 Ugh, yes, that's required.
Hi,
I want to use a constant associative array in a ctfe-able
function.
Example:
string ctfeableFunction()
{
return Foo["foo"];
}
Then I force ctfe with:
enum res = ctfeableFunction();
When I use an enum like:
enum Foo = ["foo" : "bar"];
It works fine.
D-Scanner keep saying to me: This enum may lead to unnecessary
allocation at run-time. Use 'static immutable [...] instead'
But it lead to: Error: static variable Foo cannot be read at
compile time.
So is the only way to make ctfe-able associative array is to use
enum instead of static immutable ?
Mar 19 2019
On Tuesday, 19 March 2019 at 08:50:15 UTC, boolangery wrote:
Hi,
I want to use a constant associative array in a ctfe-able
function.
Example:
string ctfeableFunction()
{
return Foo["foo"];
}
Then I force ctfe with:
enum res = ctfeableFunction();
When I use an enum like:
enum Foo = ["foo" : "bar"];
It works fine.
D-Scanner keep saying to me: This enum may lead to unnecessary
allocation at run-time. Use 'static immutable [...] instead'
But it lead to: Error: static variable Foo cannot be read at
compile time.
So is the only way to make ctfe-able associative array is to
use enum instead of static immutable ?
Yes, I think so. If you would use the enum AA multiple times, it
would allocate a new AA each time, that is wat D-Scanner warns
against. I am not sure how the CTFE interpreter is implemented,
it could be that a new AA is allocated each time you call
ctfeableFunction, at compile time. But unless you also call it at
runtime, there should be no extra run time allocations.
If you need the AA at run time as well, I would create a static
immutable version for that, initialised with the enum.
Beware that a CT AA stores its elements in a different order than
a RT AA, which you would notice in a foreach, for example.
Mar 19 2019
On 3/19/19 7:22 PM, Bastiaan Veelo wrote:Beware that a CT AA stores its elements in a different order than a RT AA, which you would notice in a foreach, for example.Yes, this is the issue -- the runtime AA depends on druntime, which is not available to the compiler. The compiler has its own AA implementation that it uses for CTFE. The internals will not be the same, which is why you can't construct one at compile-time and use it at runtime (the enum just recreates it at runtime when used). -Steve
Mar 19 2019
On Tuesday, 19 March 2019 at 23:41:58 UTC, Steven Schveighoffer wrote:On 3/19/19 7:22 PM, Bastiaan Veelo wrote:Got it ! Thank you, so I need to write: enum string[string] CtfeFoo = ["foo" : "bar"]; static immutable string[string] Foo; static this() { Foo = CtfeFoo; } string ctfeableFunction() { if (__ctfe) return CtfeFoo["foo"]; else return Foo["foo"]; } void main() { enum a = ctfeableFunction(); auto b = ctfeableFunction(); }Beware that a CT AA stores its elements in a different order than a RT AA, which you would notice in a foreach, for example.Yes, this is the issue -- the runtime AA depends on druntime, which is not available to the compiler. The compiler has its own AA implementation that it uses for CTFE. The internals will not be the same, which is why you can't construct one at compile-time and use it at runtime (the enum just recreates it at runtime when used). -Steve
Mar 20 2019
On Wednesday, 20 March 2019 at 08:11:27 UTC, boolangery wrote:
Got it ! Thank you, so I need to write:
enum string[string] CtfeFoo = ["foo" : "bar"];
static immutable string[string] Foo;
static this()
{
Foo = CtfeFoo;
}
string ctfeableFunction()
{
if (__ctfe)
return CtfeFoo["foo"];
else
return Foo["foo"];
}
void main()
{
enum a = ctfeableFunction();
auto b = ctfeableFunction();
}
Yes, that looks correct to me.
Mar 20 2019
On 3/20/19 4:11 AM, boolangery wrote:On Tuesday, 19 March 2019 at 23:41:58 UTC, Steven Schveighoffer wrote:Ugh, yes, that's required. Probably can be abstracted in a template. -SteveOn 3/19/19 7:22 PM, Bastiaan Veelo wrote:Got it ! Thank you, so I need to write: enum string[string] CtfeFoo = ["foo" : "bar"]; static immutable string[string] Foo; static this() { Foo = CtfeFoo; } string ctfeableFunction() { if (__ctfe) return CtfeFoo["foo"]; else return Foo["foo"]; }Beware that a CT AA stores its elements in a different order than a RT AA, which you would notice in a foreach, for example.Yes, this is the issue -- the runtime AA depends on druntime, which is not available to the compiler. The compiler has its own AA implementation that it uses for CTFE. The internals will not be the same, which is why you can't construct one at compile-time and use it at runtime (the enum just recreates it at runtime when used). -Steve
Mar 20 2019









Bastiaan Veelo <Bastiaan Veelo.net> 