Fake global associative array literals
sergk
kovrov+puremagic at gmail.com
Wed Nov 9 03:36:42 PST 2011
On Fri, Oct 28, 2011 at 5:53 PM, bearophile <bearophileHUGS at lycos.com> wrote:
> What do you think about a rewrite rule that changes code like:
>
> int[int] aa = [1:2, 3:4];
> void main() {}
>
>
> Into:
>
> int[int] aa;
> static this() {
> aa = [1:2, 3:4];
> }
> void main() {}
Its not quite same case, but still could be useful - what I usually do
if I need global or static immutable AA behavior:
int f_aa(int key)
{
switch (key) {
case 1: return 2;
case 3: return 4;
default: return int.init;
}
}
void main()
{
// f_aa == [1:2, 3:4]
static assert (f_aa(1) == 2);
static assert (f_aa(3) == 4);
}
For immutable data or CTFE I cannot see other way to get AA-like data.
The function itself could be generated on compile-time as string
mixin, but I usually don't bother.
--
serg.
More information about the Digitalmars-d-learn
mailing list