RedBlackTree and myClass
tsbockman via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Jan 3 08:44:35 PST 2016
On Sunday, 3 January 2016 at 16:25:31 UTC, Tobi G. wrote:
> On Sunday, 3 January 2016 at 14:49:59 UTC, tsbockman wrote:
>> Anyway, it's not too hard if you understand what's going on,
>> and all of the functions I added are good things to have
>> anyway, because lots of generic code expects some or all of
>> them. But, the error messages aren't all that helpful if you
>> didn't already know most of that.
>
> But as a beginner it is quite disappointing to write all of
> these functions to just get it work.
> Maybe i am a bad programmer, but i don't write and use the
> member functions above that often.
> I often use the 'less' in the template arguments to get such
> things as comparison done, and implement these functions only
> if i have to..
> To get it work this should be enough:
>
> import std.container.rbtree;
>
> class myClass {
> string str;
>
> override string toString() const {
> return "{myClass: " ~ str ~ "}"; }
> }
>
> void main()
> {
> auto tree = new RedBlackTree!(myClass, "a.str < b.str");
> }
>
> ~ togrue
It just depends on what the class is for.
If it's a private internal data structure which is only used a
few places, then sure - just use the minimum code required to get
the job done.
But, if it's a part of the public API for a module and the class
logically has a natural ordering, it's better to specify its
behavior once in the class definition, rather than re-implement
it everywhere that needs it.
Obviously there isn't much point to creating a class that does
nothing bug wrap `string`, so I tried to provide the generic
solution which can be easily extended to do the right thing when
the class is fleshed out to actually do whatever it is really
supposed to do.
And if you really just want a named `string` wrapper, why not do
this?
class myClass {
string str;
alias str this;
}
More information about the Digitalmars-d-learn
mailing list