yank unary '+'?
bearophile
bearophileHUGS at lycos.com
Sun Dec 6 16:46:33 PST 2009
Walter Bright:
> Think of it like the "bool" operator overload. bool gives a direct way
> for user defined times to be tested for if statements, etc. Similarly,
> U+ gives a direct way for user defined types to be converted to their
> most desired arithmetic type.
I'd like opBool in D, for example to implement multiprecision numbers that can be used as integers in:
if (somenumber) {...
Or when I want to give Python-like semantics to my collections, so they can be false when empty (I don't know if this is a good idea, I think it's handy).
I agree with Andrei that type conversions done with + are cryptic, I am not a C expert but I am using it for some time and I have never seen + used that way.
I'd like to keep writing +5.5, with numbers (and maybe with user-defined numbers too).
Unary pos operator overload is available even in Python, it's named __pos__, an answer about it, that shows two bad usages of __pos__:
http://groups.google.com/group/comp.lang.python/msg/07d6fb1e2c7a4f42
This is ugly, I didn't know about this:
>>> import decimal
>>> x = decimal.Decimal("-0.0")
>>> x
Decimal('-0.0')
>>> +x
Decimal('0.0')
>>> x = decimal.Decimal("-2.0")
>>> x
Decimal('-2.0')
>>> +x
Decimal('-2.0')
Keeping opPos allows to write +x where x is a user-defined number (and x must not change sign as in the Python Decimal zero, I don't understand that design decision), so even if it's not so useful I think it's better to keep it in D.
Bye,
bearophile
More information about the Digitalmars-d
mailing list