Associative Array Initalizers - Possible Syntax?

Xinok xnknet at gmail.com
Mon Feb 12 11:08:26 PST 2007


I've tried using this syntax as a suggestion before for initalizing associative arrays. It didn't catch on because a few people thought 'it was too much to type', and I agree.

int[int] arr = [[0] = 15, [1] = 30, [2] = 45];

Yesterday, I saw an example of Lisp, which gave me an idea to make initalizing associative arrays quicker using a similar syntax:

int[int] arr = [[0, 1, 2] = [15, 30, 45]];

Initalizing multiple elements in a single expression is much quicker than initalizing single elements. The syntax could allow you to initalize in groups of 5 or however many, so it's easy to tell which initalizer belongs to which element:

char[][int] arr = [[10, 20, 30, 40, 50] = ["Ten", "Twenty", "Thirty", "Forty", "Fifty"],
                   [60, 70, 80, 90, 100] = ["Sixty", "Seventy", "Eighty", "Ninety", "One Hundred"]];


This syntax is also nestable:
int[int][int] arr = [[0, 10, 20] = [[5] = 0, [15] = 10, [25] = 20]];


It might be easier to read if braces were used { } for the index list though:
int[int][int] arr = [{0, 10, 20} = [{5} = 0, {15} = 10, {25} = 20]];



And yes, I've already seen the associative array initalizer using mixins...



More information about the Digitalmars-d mailing list