Operator overloading, structs

bearophile bearophileHUGS at lycos.com
Mon Jun 1 17:17:39 PDT 2009


>> - In #2 D doesn't have a standard method that returns true/false. In Python2.6 such method is named __nonzero__ and in python3 it's named __bool__.<<

No one has commented about that, but I think that having a way to overload cast(bool)foo is important. I use it in Python and I have shown why and how it can be used in D too. It's an easy thing to do and I think it's safe.

Don:

> > D1 has opCast, but here I think not even a[cast(long)i] works, because BigInt doesn't define it yet. When a big int can't be converted to long, it can throw an exception.
> 
> Yes, that's not a bad idea.

But eventually an implicit cast will be better (even if a bit less safe) in D2.


> Not toString(), though. You MUST be able to specify if you want leading 
> zeros, and if you want hex or decimal.

toString() is for the default case, when you just want the decimal number with no leading zeros. Then you can add other methods to output hex and all you want.
(In what situations do you want to print leading zeros of a big multiprecision integral value? I have never faced such need so far).


> But I hate toString() so much, I'm _never_ going to support it. It's an 
> OMDB (over my dead body). Sorry.

I have patched a copy of the bigint module to add the toString method. I'll keep using such patch for my programs. I want you well alive and happy too.


> Fixed in Tango SVN 4717.

Thank you.

Bye,
bearophile



More information about the Digitalmars-d mailing list