digitalmars.D.learn - Can structs be used with associative arrays?
- Steve Adams (13/13) Jul 20 2005 From looking at the description of associative arrays, it looks like I
- Derek Parnell (17/33) Jul 20 2005 You get this because there is no x["a"] in existence yet. Try this ...
- Steve Adams (2/38) Jul 20 2005
- Ben Hinkle (4/11) Jul 20 2005 Note the *(new A) will allocate a new A and then copy the contents to x[...
- Derek Parnell (6/18) Jul 20 2005 That's neat. Thanks.
- Dejan Lekic (6/6) Jul 21 2005 Ben, thanks for this wanderfull hint! :)
- jicman (23/34) Jul 21 2005 Ben,
- Derek Parnell (11/53) Jul 21 2005 Because the .init technique was for use with structs and other value-typ...
- jicman (8/57) Jul 22 2005 That's why I was never able to get a struct working. ;-) But Classes wo...
- Tim Locke (16/27) Apr 29 2006 What is the concise method of initializing the members of my array of
- Chris Nicholson-Sauls (12/49) Apr 30 2006 The {a:1, b:2} format is the struct static initializer syntax, used with...
- Tim Locke (5/31) Apr 30 2006 Thanks for the help. I didn't expect so many options.
From looking at the description of associative arrays, it looks like I should be able to say: struct A { int a; } int main() { A[char[]] x; x["a"].a = 1; // causes a runtime error return( 0 ); } But, when compiled and run with 0.128, this generates: Error: ArrayBoundsError z.d(8)
Jul 20 2005
On Wed, 20 Jul 2005 18:57:53 -0400, Steve Adams wrote:From looking at the description of associative arrays, it looks like I should be able to say: struct A { int a; } int main() { A[char[]] x; x["a"].a = 1; // causes a runtime error return( 0 ); } But, when compiled and run with 0.128, this generates: Error: ArrayBoundsError z.d(8)You get this because there is no x["a"] in existence yet. Try this ... struct A { int a; } int main() { A[char[]] x; x["a"] = *(new A); // Create an instance of the struct. x["a"].a = 1; return( 0 ); } -- Derek Melbourne, Australia 21/07/2005 9:11:22 AM
Jul 20 2005
Thanks, that did it. wrote:On Wed, 20 Jul 2005 18:57:53 -0400, Steve Adams wrote:From looking at the description of associative arrays, it looks like I should be able to say: struct A { int a; } int main() { A[char[]] x; x["a"].a = 1; // causes a runtime error return( 0 ); } But, when compiled and run with 0.128, this generates: Error: ArrayBoundsError z.d(8)You get this because there is no x["a"] in existence yet. Try this ... struct A { int a; } int main() { A[char[]] x; x["a"] = *(new A); // Create an instance of the struct. x["a"].a = 1; return( 0 ); }
Jul 20 2005
int main() { A[char[]] x; x["a"] = *(new A); // Create an instance of the struct. x["a"].a = 1; return( 0 ); }Note the *(new A) will allocate a new A and then copy the contents to x["a"] and then throw away the allocated memory. So a way of doing the same thing without allocating extra memory is to x["a"] = A.init;
Jul 20 2005
On Wed, 20 Jul 2005 20:46:26 -0400, Ben Hinkle wrote:That's neat. Thanks. -- Derek Melbourne, Australia 21/07/2005 10:49:46 AMint main() { A[char[]] x; x["a"] = *(new A); // Create an instance of the struct. x["a"].a = 1; return( 0 ); }Note the *(new A) will allocate a new A and then copy the contents to x["a"] and then throw away the allocated memory. So a way of doing the same thing without allocating extra memory is to x["a"] = A.init;
Jul 20 2005
Ben, thanks for this wanderfull hint! :) -- ........... Dejan Lekic http://dejan.lekic.org
Jul 21 2005
Ben Hinkle says...Ben, this is not working for me. However, new is. Take this example: import std.stdio; class ReportTotals { char[] DayDate = ""; int PPHArr[23]; int FPHArr[23]; } void main() { ReportTotals Month[]; Month.length = Month.length + 1; //Month[0] = ReportTotals.init; Month[0] = new ReportTotals(); Month[0].DayDate = "Apr 6"; } if you compile this program, it works. However, if you comment out the "new" line with your suggested .init like, I get an access violation. Why is that? I want to save memory, but I also want it to work. ;-) thanks for the help. joséint main() { A[char[]] x; x["a"] = *(new A); // Create an instance of the struct. x["a"].a = 1; return( 0 ); }Note the *(new A) will allocate a new A and then copy the contents to x["a"] and then throw away the allocated memory. So a way of doing the same thing without allocating extra memory is to x["a"] = A.init;
Jul 21 2005
On Fri, 22 Jul 2005 04:55:13 +0000 (UTC), jicman wrote:Ben Hinkle says...Because the .init technique was for use with structs and other value-types and not with classes. Structs and Classes do not share the same syntax in D, which makes template writing such a PIA. For structs one would use '*(new Foo)' but for classes one uses 'new Foo'. Yucky, no? -- Derek Melbourne, Australia 22/07/2005 4:34:24 PMBen, this is not working for me. However, new is. Take this example: import std.stdio; class ReportTotals { char[] DayDate = ""; int PPHArr[23]; int FPHArr[23]; } void main() { ReportTotals Month[]; Month.length = Month.length + 1; //Month[0] = ReportTotals.init; Month[0] = new ReportTotals(); Month[0].DayDate = "Apr 6"; } if you compile this program, it works. However, if you comment out the "new" line with your suggested .init like, I get an access violation. Why is that? I want to save memory, but I also want it to work. ;-) thanks for the help. joséint main() { A[char[]] x; x["a"] = *(new A); // Create an instance of the struct. x["a"].a = 1; return( 0 ); }Note the *(new A) will allocate a new A and then copy the contents to x["a"] and then throw away the allocated memory. So a way of doing the same thing without allocating extra memory is to x["a"] = A.init;
Jul 21 2005
Derek Parnell says...On Fri, 22 Jul 2005 04:55:13 +0000 (UTC), jicman wrote:Aaaaah... It would seem to me that classes should also use it.Ben Hinkle says...Because the .init technique was for use with structs and other value-types and not with classes.Ben, this is not working for me. However, new is. Take this example: import std.stdio; class ReportTotals { char[] DayDate = ""; int PPHArr[23]; int FPHArr[23]; } void main() { ReportTotals Month[]; Month.length = Month.length + 1; //Month[0] = ReportTotals.init; Month[0] = new ReportTotals(); Month[0].DayDate = "Apr 6"; } if you compile this program, it works. However, if you comment out the "new" line with your suggested .init like, I get an access violation. Why is that? I want to save memory, but I also want it to work. ;-) thanks for the help. joséint main() { A[char[]] x; x["a"] = *(new A); // Create an instance of the struct. x["a"].a = 1; return( 0 ); }Note the *(new A) will allocate a new A and then copy the contents to x["a"] and then throw away the allocated memory. So a way of doing the same thing without allocating extra memory is to x["a"] = A.init;Structs and Classes do not share the same syntax in D, which makes template writing such a PIA.That's why I was never able to get a struct working. ;-) But Classes worked right away with "new Blah();".For structs one would use '*(new Foo)' but for classes one uses 'new Foo'. Yucky, no?Indeed. I would have set them the same. But I still love D. I guess no language can be perfect. :-) thanks for the explanation. josé
Jul 22 2005
On Wed, 20 Jul 2005 20:46:26 -0400, "Ben Hinkle" <ben.hinkle gmail.com> wrote:What is the concise method of initializing the members of my array of structs? It seems long-handed to do this: x["a"].a = 1; x["a"].b = 2; etc. Can this be shortened? According to spec_DMD_0.109.pdf, pages 100-101, I *think* I should be able to do something like this: x["a"] = {a:1, b:2}; But the compiler gives me the following error: "expression expected, not '{'". I also tried replacing {} with () and got "found ':' when expecting ')'". Thank you. -- On the Internet, no one knows you're [using] a VIC-20.int main() { A[char[]] x; x["a"] = *(new A); // Create an instance of the struct. x["a"].a = 1; return( 0 ); }Note the *(new A) will allocate a new A and then copy the contents to x["a"] and then throw away the allocated memory. So a way of doing the same thing without allocating extra memory is to x["a"] = A.init;
Apr 29 2006
Tim Locke wrote:On Wed, 20 Jul 2005 20:46:26 -0400, "Ben Hinkle" <ben.hinkle gmail.com> wrote:The {a:1, b:2} format is the struct static initializer syntax, used with static and const declerations only, sadly. Hopefully in the future it becomes more general purpose (related issues to array literals). While it isn't perfect, you can do this, which at least reduces the number of array lookups to one: In fact, I guess you could technically even do: So long as with() doesn't require braces after it. (I think it does, mostly because nobody ever thought of a case where you wouldn't need them... suppose I just did.) -- Chris Nicholson-SaulsWhat is the concise method of initializing the members of my array of structs? It seems long-handed to do this: x["a"].a = 1; x["a"].b = 2; etc. Can this be shortened? According to spec_DMD_0.109.pdf, pages 100-101, I *think* I should be able to do something like this: x["a"] = {a:1, b:2}; But the compiler gives me the following error: "expression expected, not '{'". I also tried replacing {} with () and got "found ':' when expecting ')'". Thank you. -- On the Internet, no one knows you're [using] a VIC-20.int main() { A[char[]] x; x["a"] = *(new A); // Create an instance of the struct. x["a"].a = 1; return( 0 ); }Note the *(new A) will allocate a new A and then copy the contents to x["a"] and then throw away the allocated memory. So a way of doing the same thing without allocating extra memory is to x["a"] = A.init;
Apr 30 2006
On Sun, 30 Apr 2006 02:41:43 -0500, Chris Nicholson-Sauls <ibisbasenji gmail.com> wrote:Tim Locke wrote:Thanks for the help. I didn't expect so many options. -- On the Internet, no one knows you're [using] a VIC-20.What is the concise method of initializing the members of my array of structs? It seems long-handed to do this: x["a"].a = 1; x["a"].b = 2; etc. Can this be shortened? According to spec_DMD_0.109.pdf, pages 100-101, I *think* I should be able to do something like this: x["a"] = {a:1, b:2}; But the compiler gives me the following error: "expression expected, not '{'". I also tried replacing {} with () and got "found ':' when expecting ')'".The {a:1, b:2} format is the struct static initializer syntax, used with static and const declerations only, sadly. Hopefully in the future it becomes more general purpose (related issues to array literals). While it isn't perfect, you can do this, which at least reduces the number of array lookups to one: In fact, I guess you could technically even do: So long as with() doesn't require braces after it. (I think it does, mostly because nobody ever thought of a case where you wouldn't need them... suppose I just did.)
Apr 30 2006