www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - using tuple as value type for associative array

reply Flaze07 <christianseiji.cs gmail.com> writes:
when I do
Tuple!(uint, "first", uint, "second")[string] what; //I tried 
aliasing the tuple as well
what["something"].first = 20;
I get range error
but when I do
uint[string] what2;
what2 = 20;
I get none of those range error, so...how do I use tuple as value 
type for associative array ?
Jun 20 2018
parent reply Computermatronic <computermatronic gmail.com> writes:
On Thursday, 21 June 2018 at 02:44:12 UTC, Flaze07 wrote:
 when I do
 Tuple!(uint, "first", uint, "second")[string] what; //I tried 
 aliasing the tuple as well
 what["something"].first = 20;
 I get range error
 but when I do
 uint[string] what2;
 what2 = 20;
 I get none of those range error, so...how do I use tuple as 
 value type for associative array ?
what["something"].first = 20 will attempt to get an element of what, then assign a member, while what2["something"] = 20 will add an element to what2 with the value of 20. Since what["something"] is not present, it will throw a range error. Try what["something"] = tuple(20, 0); instead.
Jun 20 2018
parent Flaze07 <christianseiji.cs gmail.com> writes:
On Thursday, 21 June 2018 at 03:04:46 UTC, Computermatronic wrote:
 On Thursday, 21 June 2018 at 02:44:12 UTC, Flaze07 wrote:
 when I do
 Tuple!(uint, "first", uint, "second")[string] what; //I tried 
 aliasing the tuple as well
 what["something"].first = 20;
 I get range error
 but when I do
 uint[string] what2;
 what2 = 20;
 I get none of those range error, so...how do I use tuple as 
 value type for associative array ?
what["something"].first = 20 will attempt to get an element of what, then assign a member, while what2["something"] = 20 will add an element to what2 with the value of 20. Since what["something"] is not present, it will throw a range error. Try what["something"] = tuple(20, 0); instead.
huh, interesting, thanks
Jun 20 2018