Why is operator overloading like this in D?
daniel.keep+lists at gmail.com
Mon Dec 11 20:42:51 PST 2006
> Thank you very much Daniel!
> I wonder how to quote when posting?
I use a real news client, so I hit the "Reply" button :3
> You wrote: (I quoted it manually :)
>>If you really *do* need to copy the data a structure is pointing to, my
>>suggestion would be to implement "dup" member functions. It's a little
>>inconvenient, but it means that you can always count on "=" just doing a
>>bit copy, and not something more.
> It seems that dup is not a member of Object according to the library reference,
> why? Is it because not everything can be duplicated?
Because it's not part of the library. "dup" is a property of arrays,
and sort of got extended to other things.
You could name the method "copy" or "duplicate" or "frobnicate"--it's
just a name, afterall. As long as you use it consistantly, it shouldn't
be a problem.
> On the other hand, as there are many classes with which less or greater than
> comparisons (even equality tests) makes no sense, why opCmp becomes a "default"?
> Resulting in a requirement to make arguments of opCmp be of type Object when
> ( Maybe I'm just feeling unused to test the type of objects inside the functions. :)
> If not overloaded, opEquals does a bit comparison on everything, doesn't it?
> And from the reference, I get: "If a struct has no opCmp() function declared for
> it, attempting to compare two structs is an error."
> And what does the default opCmp do with classes?
I'll leave that to Frits, who has already answered it :)
More information about the Digitalmars-d-learn