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]; }

Reply via email to