How do I use CTFE to generate an immutable associative array at compile time?

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 22 07:27:22 PST 2017


On Wed, Feb 22, 2017 at 09:00:36AM +0100, Jacob Carlborg via Digitalmars-d-learn wrote:
[...]
> You can use an enum to declare the AA and then assign it to an
> immutable variable using "static this". The you would only use to the
> immutable variable and never the enum.
> 
> enum aa = [1 : 2];
> 
> immutable int[int] iaa;
> 
> static this()
> {
>     iaa = aa;
> }
[...]

Wow, this is miles better than the hacks that I've come up with!

Also makes the no-static-AA issue much less of a problem that I thought
it was.  (In fact, now I'm wondering if we could just hack dmd to emit
the equivalent of this code as a lowering, whenever the user tries to
declare a compile-time initialized AA.)


T

-- 
Computers are like a jungle: they have monitor lizards, rams, mice, c-moss, binary trees... and bugs.


More information about the Digitalmars-d-learn mailing list