use of struct vs class

mark mark at qtrac.eu
Sat Mar 7 10:20:25 UTC 2020


I have this struct (with details omitted ... for brevity):

struct Deb {
     string name;
     ...
     RedBlackTree!string tags;

     void clear() { name = ""; ...; tags.clear; }

     bool valid() { return !(name.empty || description.empty); }
}

I plan to store >65K of these (with potential for growth to 
 >250K) in an AA:

Deb[string] debForName;

I plan to populate debForName by reading data files (actually 
Debian Packages files) like this:

Deb deb;
auto file = File(filename):
foreach(line; file.byLine) {
     if (line.empty) {
         if (deb.valid) // end of package
             debForName[deb.name] = deb; // XXX
         // else report incomplete package
         deb.clear;
         continue;
     }
     ... // populate the deb
}
if (deb.valid)
     debForName[deb.name] = deb;

I'm assuming that line XXX will copy the Deb including the tree 
(which as usual I'm using as a set -- I really miss a set class 
in D!). Will this work (I'll find out myself next week when I get 
further, but D experts can likely tell from the above).

Should Deb be a class rather than a struct?


More information about the Digitalmars-d-learn mailing list