Why can't I use a map's value without having to use a temporary variable?

StackOverflow https://stackoverflow.com/questions/16318133

  •  13-04-2022
  •  | 
  •  

Domanda

Ok so this is my scenario:

rascal>map[int, list[int]] g = ();
rascal>g += (1:[2]);

This will result in:

rascal>g[1];
list[int]: [2]

So far so good, but now I wanted to do this, but it didn't work:

rascal>g[1] += 3;
|stdin:///|(2,1,<1,2>,<1,3>): insert into collection not supported on value and int

So I can't directly use the value from g[1] and will have to use a temporary variable like this:

rascal>lst = g[1];
rascal>lst += 3;
rascal>g[1] = lst;
map[int, list[int]]: (1:[2,3])

But doing this everytime I want to extent my list is a drag! Am I doing something wrong or would this be an awesome feature?

Richard

È stato utile?

Soluzione

Good question! + on lists is concatenation not insert, so you could type the following to get the desired effect:

g[1] += [2];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top