On Wednesday, 26 June 2013 at 05:29:49 UTC, Jonathan M Davis
wrote:
On Wednesday, June 26, 2013 07:15:19 Eric wrote:
I should have also added that the overloaded ! method returns a
class instance and not a bool.
Well, at that point, you're completely out of luck with regards
to overloading
!. In general, D doesn't really support overloading operators
in a manner that
doesn't match how the built-in types work. It does things like
assume that <,
<=, >=, and > will act the same way that they do with the
built-in types and
uses opCmp to extrapolate all of those operators. In some
cases, you _can_
make overloaded operators return types that make it so that
they don't
function anything like the built-in types (e.g. opBinary with +
could return a
completely unrelated type that had nothing to do with the
original type or
with addition), but in pretty much any case where the compiler
can get away
with using the same overloaded operator for multiple operators,
it does it.
So, a number of decisions were made to better support
correctness with types
that actually try and overload operators to match what the
built-in types do
without caring about how that would affect types that would try
and overload
them to do unrelated stuff.
- Jonathan M Davis
Yeah, basically, you define:
* opCast(bool) //to test both "true" and "false"
* opEqual //test equality (both == and !=)
* opCmp //Comparison (< > <= >=)
And everything gets defined from these. It also works that way
for operators:
op++ is defined in terms or ++op.
And if you define "op+", then defining op++ becomes entirely
optional!
This is a cool feature, although there are those *very* rare
cases where it gets in your way.
For example, the other day, I was trying to implement a "tribool":
http://www.boost.org/doc/libs/1_53_0/doc/html/tribool/tutorial.html
It did not work, I mean: AT ALL!
This is because for a tribool, being "not true" does not imply
being "false"...