That's obscure.
int[int] map1 => a map from int -> int
int[int][int] map2 => a map from int -> (map from int -> int)
int[int, int] map3 => (the same)
In Java, when you fetch from a map, if the element is not present, you get a null.
In ASH, when you fetch from a map, if the element is not present, you get the default for the value type.
If you have a 2-dimensional map, as above:
map3[1, 2} => 0 (default for int)
map3[1] => an anonymous int[int] map (default for int[int])
The issue is that ASH will create intermediate "slices", when fetching, but will not save the final slice.
Here's an example script:
yields
(A map from a key to another map is the same as a multi-dimensional map)
yields
(Fetching from a single-dimension map with a non-existent key does not add the key)
yields
(fetching from a multi-dimensional map without using all the indices gives you a "slice" - but does not put it in the map.
(This is the issue)
yields
(Fetching using all the indices when the first one does not exist will create the first slice, but not the second.)
(The same issue)
Here is an example of how this is a pitfall for the ASH coder:
yields
The ASH program intentionally fetched an intermediate slice.
It modified it.
It might have hoped that it was modifying the original map, but no.
If the original key was not present, it must store it back in the original map.
Perhaps like this:
or perhaps like this:
In Java, it would be obvious you have to do this; fetching a value from a map with a non-existent key returns null - and if it is a mutable value which you intend to change, you have to create a new one and store it back in the map.
ASH does not have null. It would simplify things for even experienced coders if fetching a mutable element using a non-existent key not only returned the default value, but also stored it in the map with that key.
int[int] map1 => a map from int -> int
int[int][int] map2 => a map from int -> (map from int -> int)
int[int, int] map3 => (the same)
In Java, when you fetch from a map, if the element is not present, you get a null.
In ASH, when you fetch from a map, if the element is not present, you get the default for the value type.
If you have a 2-dimensional map, as above:
map3[1, 2} => 0 (default for int)
map3[1] => an anonymous int[int] map (default for int[int])
The issue is that ASH will create intermediate "slices", when fetching, but will not save the final slice.
Here's an example script:
Code:
int[int] map1;
int[int][int] map2;
int[int,int] map3;
print(map1);
print(map2);
print(map3);
Code:
aggregate int [int]
aggregate int [int, int]
aggregate int [int, int]
Code:
print("map1 contains 1 = " + (map1 contains 1) +
" map1[1] = " + map1[1] +
" map1 contains 1 = " + (map1 contains 1));
Code:
map1 contains 1 = false map1[1] = 0 map1 contains 1 = false
Code:
print("count(map2) = " + count(map2) +
" map2[1] = " + map2[1] +
" count(map2) = " + count(map2));
Code:
count(map2) = 0 map2[1] = aggregate int [int] count(map2) = 0
(This is the issue)
Code:
print("count(map2) = " + count(map2) +
" count(map2[1]) = " + count(map2[1]) +
" map2[1][2] = " + map2[1][2] +
" count(map2) = " + count(map2) +
" count(map2[1]) = " + count(map2[1]));
Code:
count(map2) = 0 count(map2[1]) = 0 map2[1][2] = 0 count(map2) = 1 count(map2[1]) = 0
(The same issue)
Here is an example of how this is a pitfall for the ASH coder:
Code:
int[int][int] map4;
print("map4 contains 1 = " + (map4 contains 1));
int[int] map5 = map4[1];
print("(after fetch of map4[1]) map4 contains 1 = " + (map4 contains 1) +
" map5 = " + map5 +
" map5 contains 2 = " + (map4 contains 2));
map5[2] = 10;
print("(after store of map5[2]) map5 contains 2 = " + (map5 contains 2) +
" map4[1] contains 2 = " + (map4[1] contains 2))
Code:
map4 contains 1 = false
(after fetch of map4[1]) map4 contains 1 = false map5 = aggregate int [int] map5 contains 2 = false
(after store of map5[2]) map5 contains 2 = true map4[1] contains 2 = false
The ASH program intentionally fetched an intermediate slice.
It modified it.
It might have hoped that it was modifying the original map, but no.
If the original key was not present, it must store it back in the original map.
Perhaps like this:
Code:
int[int][int] map;
int[int] data = map[1];
if (!(map contains 1)) {
map[1] = data;
}
data[2] = 10;
or perhaps like this:
Code:
int[int][int] map;
int[int] data = map[1];
data[2] = 10;
map[1] = data
In Java, it would be obvious you have to do this; fetching a value from a map with a non-existent key returns null - and if it is a mutable value which you intend to change, you have to create a new one and store it back in the map.
ASH does not have null. It would simplify things for even experienced coders if fetching a mutable element using a non-existent key not only returned the default value, but also stored it in the map with that key.