use of struct vs class

mark mark at qtrac.eu
Sat Mar 7 10:45:44 UTC 2020


On Saturday, 7 March 2020 at 10:30:06 UTC, drug wrote:
> 07.03.2020 13:20, mark пишет:
>> I have this struct (with details omitted
>> [ snip ]
>> Should Deb be a class rather than a struct?
>
> Do you consider using pointers in AA:
> ```
> Deb*[string] debForName;
> ```

I've done some changes including using Deb* as you suggested:

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

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

     size_t toHash() const @safe nothrow {
         return typeid(name).getHash(&name); // names are unique
     }

     bool opEquals(const Deb other) const @safe pure nothrow {
         return name == other.name; // names are unique
     }

     int opCmp(ref const Deb other) const {
         return cmp(name, other.name); // names are unique
     }
}

Which I now want to store in:

RedBlackTree!Deb* debs; // name-ordered list of deb packages

And now I'm populating like this:

Deb* deb;
auto file = File(filename);
foreach(line; file.byLine) {
     line = strip(line);
     if (line.empty) {
         if (deb != null && deb.valid) {
             debs.insert(deb);
             deb.clear;
         }
         // else report incomplete package
         continue;
     }
     if (deb == null)
         deb = new Deb;
     ...
}
if (deb != null && deb.valid)
     debs.insert(deb);

But it crashes with:

Performing "debug" build using 
/home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/ldc2 for x86_64.
gtk-d:gtkd 3.9.0: target for configuration "library" is up to 
date.
debfind ~master: building configuration "application"...
src/model.d(96,36): Error: template 
std.container.rbtree.RedBlackTree!(Deb, "a < b", 
false).RedBlackTree.stableInsert cannot deduce function from 
argument types !()(Deb*), candidates are:
/home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/../import/std/container/rbtree.d(1256,12):        stableInsert(Stuff)(Stuff stuff)
   with Stuff = Deb*
   must satisfy the following constraint:
        isImplicitlyConvertible!(Stuff, Elem)
...




More information about the Digitalmars-d-learn mailing list