Additional Binary Operators
Paul D. Anderson
paul.d.removethis.anderson at comcast.andthis.net
Fri Mar 1 13:51:58 PST 2013
Operator overloading in D is straightforward and sensible.
I think there is an opportunity for D to do better with some very
small changes.
I propose including additional Unicode mathematical symbols as
recognized operators for opBinary. All that would be required is
to extend the lexer to recognize the symbols as binary operators.
For example, in vector arithmetic there are two different
products defined -- the dot product and the cross product. Most
implementations I've seen overload '*' for the inner product, but
omit operator for the cross product. In UTF-8 the "middle dot",
('•', \u00B7) and "multiplication sign", ('×', \u00D7),
correspond to the more usual mathematical notation for the dot
and cross products, respectively.
UTF-16 has many more mathematical symbols, particularly on the
2200-22FF and 2A00-2AFF code pages. \u2207 is a wedge, commonly
used for outer products. The circled plus (u\2A01) and circled
times sign (\2A02) are defined for some mathematical operations.
\u2A33 is the "smash product" symbol, which I've never heard of
before but it sounds cool.
I am NOT suggesting that we include ALL these mathematical
symbols as operators. If it was up to me I'd include just the
middle dot, the cross product and the wedge product, but YMMV.
These new operators would need a precedence. Since most of these
are product operators the precedence should be the same as '*' or
"/".
Pros:
1. Extends the capability to define mathematical operators for
user-defined types
2. Shouldn't break any existing code since the symbols aren't
valid chars in identifiers (as far as I can tell) and aren't
being used elsewhere.
3. Takes advantage of the existing opBinary overloading so
implementation should be straightforward.
Cons:
1. They are hard to type or otherwise enter in to a text file.
2. They may not display correctly in some output forms.
3. They could be confusing to someone unfamiliar with the concept.
With regard to the difficulty of entering or displaying these
characters, I think that is largely because modern computer
languages only give lip service to Unicode -- code is almost
exclusively written in ASCII. I have seen one instance where a
certain mathematical constant was named 'π', but the author was
an iconoclast and pariah.
More information about the Digitalmars-d
mailing list