Why Java Doesn't Need Operator Overloading (and Very Few Languages
Yigal Chripun
yigal100 at gmail.com
Fri Apr 17 04:46:07 PDT 2009
On 16/04/2009 15:47, Daniel Keep wrote:
>
> Yigal Chripun wrote:
>> On 15/04/2009 18:50, BCS wrote:
>>> Hello Yigal,
>>>
>>>
>>>> sounds silly to me. Why not simply generalize and allow
>>>> defining in-fix functions like in functional languages? that
>>>> also includes allowing any unicode character[s].
>>> lexing and (more importantly) parsing become a major problem if
>>> you allow that
>>>
>>>
>> Since it is done in other languages (for example Scala), I'm sure
>> it's not impossible to implement. I don't think Scala is that much
>> more troublesome to lex/parse.
>
> That's not a very good argument. Lots of things are possible;
> doesn't mean they're a good idea.
>
> Operator precedence determines how expressions are parsed. Having
> user-defined operators would require the code to be parsed once to
> get the operator definitions, then re-parsed with a different
> meaning. Euch.
>
> That said, I'm OK with adding operators with well-defined meanings
> to the language on an as-needed basis. opDot, opUnion, opCross,
> etc.
>
> -- Daniel
say we have the expression (2 + 3 foo 4) (foo is infix function).
Scala parses this as: 2 + 3.foo(4) so there is no parsing problems with
this.
see http://www.scala-lang.org/node/118
Eiffel is similar (also an OOP approach):
2 + 4 is the same as 2.plus(4)
the definition of plus is (alias is used to specify the operator):
> plus alias "+" (other: INTEGER): INTEGER
> -- ... Normal function declaration...
> end
from wikipedia for Eiffel:
>
> The range of operators that can be used as "alias" is quite broad;
> they include predefined operators such as "+" but also "free
> operators" made of non-alphanumeric symbols. This makes it possible
> to design special infix and prefix notations, for example in
> mathematics and physics applications.
>
> Every class may in addition have one function aliased to "[]", the
> "bracket" operator, allowing the notation a [i, ...] as a synonym for
> a.f (i, ...) where f is the chosen function. This is particularly
> useful for container structures such as arrays, hash tables, lists
> etc.
in Haskell you can define both precedence and associativity for
operators. I don't know Haskell but found this link which explains the
syntax:
http://www.haskell.org/tutorial/functions.html
I guess we need someone who knows Haskell to explain how they
implemented this.
More information about the Digitalmars-d
mailing list