digitalmars.D.learn - struct or class
- nikki (33/33) Aug 24 2014 I come from languages that don't offer structs, I have this json
- Rikki Cattermole (9/13) Aug 24 2014 Here's a simple way of working it out.
- FreeSlave (10/44) Aug 24 2014 Your struct instance will occupy only 24 bytes. It's ok even if
- ketmar via Digitalmars-d-learn (11/14) Aug 24 2014 this will copy:
- ketmar via Digitalmars-d-learn (6/9) Aug 24 2014 p.s.
- Kiith-Sa (7/41) Aug 24 2014 In this case class makes sense (assuming AtlasSpriteData has few
I come from languages that don't offer structs, I have this json load function that has to keep some data and intuitively I've written a struct, I've read about the differences, heap vs stack, value vs reference, but know I think i am overthinking it. Is this decent: bool loadFromFile (string path) { auto data = readText(path); JSONValue parsed = parseJSON(data); struct AtlasSpriteData { SDL_Rect clipRectangle; int xOffset; int yOffset; } AtlasSpriteData[string] dict; foreach( string name, value; parsed["frames"] ){ SDL_Rect clipRectangle; auto spriteSourceSize = value["spriteSourceSize"]; clipRectangle.x = to!int(frame["x"].toString()); clipRectangle.y = to!int(frame["y"].toString()); clipRectangle.w = to!int(frame["w"].toString()); clipRectangle.h = to!int(frame["h"].toString()); int xOffset = to!int(spriteSourceSize["x"].toString()); int yOffset = to!int(spriteSourceSize["y"].toString()); auto data = AtlasSpriteData(clipRectangle, xOffset, yOffset); dict[name] = data; } Or should I use a class for that AtlasSpriteData? reading about it I get the impression everytime I'll look up data from that dictionary data will get copied ?
Aug 24 2014
On 24/08/2014 11:56 p.m., nikki wrote:I come from languages that don't offer structs, I have this json load function that has to keep some data and intuitively I've written a struct, I've read about the differences, heap vs stack, value vs reference, but know I think i am overthinking it.Here's a simple way of working it out. If you're using arrays of a type, use classes. If you're storing lots of data inside it, classes. If you need inheritance, classes. If you have simple data for returning/argument passing then struct. Basically, small, short lived allocations stack. Long lived, large allocations, classes. At least that's my opinion.
Aug 24 2014
On Sunday, 24 August 2014 at 11:56:44 UTC, nikki wrote:I come from languages that don't offer structs, I have this json load function that has to keep some data and intuitively I've written a struct, I've read about the differences, heap vs stack, value vs reference, but know I think i am overthinking it. Is this decent: bool loadFromFile (string path) { auto data = readText(path); JSONValue parsed = parseJSON(data); struct AtlasSpriteData { SDL_Rect clipRectangle; int xOffset; int yOffset; } AtlasSpriteData[string] dict; foreach( string name, value; parsed["frames"] ){ SDL_Rect clipRectangle; auto spriteSourceSize = value["spriteSourceSize"]; clipRectangle.x = to!int(frame["x"].toString()); clipRectangle.y = to!int(frame["y"].toString()); clipRectangle.w = to!int(frame["w"].toString()); clipRectangle.h = to!int(frame["h"].toString()); int xOffset = to!int(spriteSourceSize["x"].toString()); int yOffset = to!int(spriteSourceSize["y"].toString()); auto data = AtlasSpriteData(clipRectangle, xOffset, yOffset); dict[name] = data; } Or should I use a class for that AtlasSpriteData? reading about it I get the impression everytime I'll look up data from that dictionary data will get copied ?Your struct instance will occupy only 24 bytes. It's ok even if you will copy it. I would avoid heap allocation in this case. Also what is 'frame' variable? I don't see local declaration of it. Or you just forgot to replace 'value' with 'frame'. Does not JSONValue.integer fit in this case instead of to!int(JSONValue.toString()) ? Reading does not perform copy if you access struct directly as dict[name].some_field. Copying is performed only if you pass struct by value or assign it to variable.
Aug 24 2014
On Sun, 24 Aug 2014 11:56:42 +0000 nikki via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Or should I use a class for that AtlasSpriteData? reading about it I get the impression everytime I'll look up data=20 from that dictionary data will get copied ?this will copy: auto sd =3D dict[0]; this will copy: foreach (sd; dict) { ... } this will not: const *sd =3D &dict[0]; this will not: foreach (ref sd; dict) { ... } hope you got the idea.
Aug 24 2014
On Sun, 24 Aug 2014 11:56:42 +0000 nikki via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Or should I use a class for that AtlasSpriteData? reading about it I get the impression everytime I'll look up data=20 from that dictionary data will get copied ?p.s. this will not copy: auto sd =3D "mysprite00" in dict; 'sd' is of type 'AtlasSpriteData*' here.
Aug 24 2014
On Sunday, 24 August 2014 at 11:56:44 UTC, nikki wrote:I come from languages that don't offer structs, I have this json load function that has to keep some data and intuitively I've written a struct, I've read about the differences, heap vs stack, value vs reference, but know I think i am overthinking it. Is this decent: bool loadFromFile (string path) { auto data = readText(path); JSONValue parsed = parseJSON(data); struct AtlasSpriteData { SDL_Rect clipRectangle; int xOffset; int yOffset; } AtlasSpriteData[string] dict; foreach( string name, value; parsed["frames"] ){ SDL_Rect clipRectangle; auto spriteSourceSize = value["spriteSourceSize"]; clipRectangle.x = to!int(frame["x"].toString()); clipRectangle.y = to!int(frame["y"].toString()); clipRectangle.w = to!int(frame["w"].toString()); clipRectangle.h = to!int(frame["h"].toString()); int xOffset = to!int(spriteSourceSize["x"].toString()); int yOffset = to!int(spriteSourceSize["y"].toString()); auto data = AtlasSpriteData(clipRectangle, xOffset, yOffset); dict[name] = data; } Or should I use a class for that AtlasSpriteData? reading about it I get the impression everytime I'll look up data from that dictionary data will get copied ?In this case class makes sense (assuming AtlasSpriteData has few instances that will be shared around a lot). But you could also use a pointer to a struct, especially if you manually allocate it and want to avoid the GC. Also, you can read data from the associative array by reference (basic example, no error checking): ref AtlasSpriteData spriteData(string name) { return dict[name]; }
Aug 24 2014