Wrong lowering for a[b][c]++

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Mar 21 12:11:53 PDT 2012


On Wed, Mar 21, 2012 at 07:56:47PM +0100, Andrej Mitrovic wrote:
> On 3/21/12, H. S. Teoh <hsteoh at quickfur.ath.cx> wrote:
> > 	int[string][int] map;
> > 	map["abc"] = int[int].init;
> > 	map["abc"][30] = 123;
> 
> I think you meant:
> 
>     int[int][string] map;
>     map["abc"] = (int[int]).init;
>     map["abc"][30] = 123;

Yes, I did. :-)


> You can however init with null if the value is a hash:
> 
>     int[int][string] map;
>     map["abc"] = null;
>     map["abc"][30] = 123;
> 
> But otherwise I agree it would be nice if there was a shortcut for this.

Without this "shortcut", it's a complete pain to deal with deeply-nested
AA's:

	int[int][int][string] map;

	void inc_frequency(string entry, int x, int y) {
		if (entry in map) {
			if (x in map[entry]) {
				if (y in map[entry][x]) {
					map[entry][x][y]++;
				} else {
					map[entry][x][y] = 1;
				}
			} else {
				map[entry][x] = null;
				map[entry][x][y] = 1;
			}
		} else {
			map[entry] = null;
			map[entry][x] = null;
			map[entry][x][y] = 1;
		}
	}

whereas if this was supported, the code would simply be:

	void inc_frequency(string entry, int xcoor, int ycoor) {
		map[entry][x][y]++;
	}

(And the above example is only 3 levels deep... it gets exponentially
worse with every level of nesting.)


T

-- 
Mediocrity has been pushed to extremes.


More information about the Digitalmars-d mailing list