Why Java Doesn't Need Operator Overloading (and Very Few Languages
bearophile
bearophileHUGS at lycos.com
Wed Apr 15 00:52:28 PDT 2009
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.
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