www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Associative array of const items

reply Jonathan Marler <johnnymarler gmail.com> writes:
Is there a way to have an associative array of const values? I 
thought it would have been:

const(T)[K] map;
map[x] = y;

but the second line gives Error: cannot modify const expression.  
I would think that the const(T)[K] would behave similarly to 
const(T)[], where you can modify the array, just not the 
individual elements, but associative arrays don't seem to have 
the same semantics.  Is there a way to achieve these semantics 
with an associative array?
Jun 30 2016
next sibling parent Q. Schroll <qs.il.paperinik gmail.com> writes:
On Thursday, 30 June 2016 at 17:08:45 UTC, Jonathan Marler wrote:
 Is there a way to have an associative array of const values? I 
 thought it would have been:

 const(T)[K] map;
 map[x] = y;

 but the second line gives Error: cannot modify const 
 expression.  I would think that the const(T)[K] would behave 
 similarly to const(T)[], where you can modify the array, just 
 not the individual elements, but associative arrays don't seem 
 to have the same semantics.  Is there a way to achieve these 
 semantics with an associative array?
It is possible to initialize the array with a AA literal as const(int)[string] aa = [ "1": 1, "2": 2 ]; but AAs don't have opIndexAssign etc. The reasonable things you do to const(T)[] is shorten it or reassign it completely. For an AA, this is removing elements or adding new ones. While the first is without problems, the problem is how to determine if aa["1"] = 1 is a (legal) initialization or (illegal) reassignment of a const(T). Unfortunately, there is no function to add key-value-pairs that throws an Error if the key is already there or else reinitializes the value.
Jun 30 2016
prev sibling parent reply QAston <qaston gmail.com> writes:
On Thursday, 30 June 2016 at 17:08:45 UTC, Jonathan Marler wrote:
 Is there a way to have an associative array of const values? I 
 thought it would have been:

 const(T)[K] map;
 map[x] = y;

 but the second line gives Error: cannot modify const 
 expression.  I would think that the const(T)[K] would behave 
 similarly to const(T)[], where you can modify the array, just 
 not the individual elements, but associative arrays don't seem 
 to have the same semantics.  Is there a way to achieve these 
 semantics with an associative array?
You can use std.typecons.Rebindable!(const T)[K].
Jun 30 2016
parent Jonathan Marler <johnnymarler gmail.com> writes:
On Friday, 1 July 2016 at 06:57:59 UTC, QAston wrote:
 On Thursday, 30 June 2016 at 17:08:45 UTC, Jonathan Marler 
 wrote:
 Is there a way to have an associative array of const values? I 
 thought it would have been:

 const(T)[K] map;
 map[x] = y;

 but the second line gives Error: cannot modify const 
 expression.  I would think that the const(T)[K] would behave 
 similarly to const(T)[], where you can modify the array, just 
 not the individual elements, but associative arrays don't seem 
 to have the same semantics.  Is there a way to achieve these 
 semantics with an associative array?
You can use std.typecons.Rebindable!(const T)[K].
I think this may have been exactly what I was looking for. Cool that the language supports adding these kind of semantics through a library. I'll try it out and see if it works. Thanks for the tip.
Jul 01 2016