Why Java Doesn't Need Operator Overloading (and Very Few Languages
Don
nospam at nospam.com
Wed Apr 15 02:19:09 PDT 2009
bearophile wrote:
> Don:
>> **Don't overload arithmetic operators unless you are doing arithmetic.**<
>
> Are there few non-arithmetic operators (that will support operator overloading too) with a clear semantics that can be added to D?
> The main problems here are that such operators are very few, they may end being used only in special situations, so may be unworth being added to the language, and of course ASCII has very few symbols fit for such purpose.
Not many. I did look through all the unicode symbols, and even there,
there's not many you'd want. Generally, unary operations aren't
necessary (a function call looks quite OK).
__dot, __cross would be the most useful. They are arithmetic, of course.
Set operations.
__union, __intersection, __subset, __superset, __subseteq, __superseteq.
After that things get really obscure and pretty hard to justify. But
__dot would be extremely valuable, I think. Especially since we have
array operations in the language, and a[]*b[] is defined as element-wise
multiplication.
An IDE/text editor could translate __dot into the relevant Unicode
character.
>
> For example I have implemented in D the Python sets with (almost) the same semantics (it's not equal because the operator overlading of D is a bit less powerful than the Python one. No one has so far given me an answer about this silly limit). Some of their operators/methods are:
>
> s.issubset(t) s <= t test whether every element in s is in t
> s.issuperset(t) s >= t test whether every element in t is in s
> s.union(t) s | t new set with elements from both s and t
> s.intersection(t) s & t new set with elements common to s and t
> s.difference(t) s - t new set with elements in s but not in t
> s.symmetric_difference(t) s ^ t new set with elements in either s or t but not both
>
> So you end using <= => | & ^ - for set operations too, but in real math you want to use a different set of symbols here. And such standard set of symbols is more readable (and unambiguous) compared to that ASCII mapping. APL (and the following languages like J/K/etc) is unreadable also because the symbols it uses are not standard outside the language.
>
> Bye,
> bearophile
More information about the Digitalmars-d
mailing list