4 ms·
After a few years in Scala, where all operators are methods and vice versa (2 + 3 is just sugar for 2.+(3)), the method-operator distinction just seems weird.
by lmm 13y ago
After a few years in Scala, where all operators are methods and vice versa (2 + 3 is just sugar for 2.+(3)), the method-operator distinction just seems weird.
- RodericDay 13y agothis is really cool. it makes a lot of sense to me (never touched Scala)
- draegtun 13y agoSmalltalk, Self, Ruby, Io and (i think) Perl6 are examples of other languages that also do this.
- hesselink 13y agoHaskell also has first-class operators. Operators are infix, surrounding them with parentheses makes them prefix: (+) 1 2. Functions are prefix, surrounding them with backticks makes them infix: 1 `plus` 2. You can define your own, and define precedence and left/right associativity.
- TazeTSchnitzel 13y agoAlso, all methods are curried by default, meaning you can do this: map (*2) [1,2,3,4,5] :)
- draegtun 13y agoRebol is similar where it turns its (pre-defined) infix operators into function calls. For eg. 1 + 1 is transformed into... add 1 1
- seertaak 13y agoI don't think it's really that different -- either Scala does some magic to desugar the operators into method calls taking into account operator preference, or you're going to get results that don't match expectations. If latter, then like Rebol operators don't match (mathematical) expectation. If former, then there is actually a distinction between methods and operators in Scala, so Scala is just like Python. Or am I missing something?
- lmm 13y agoIt means that "overloading" an operator in Scala is not a special case - downthread someone was saying this addition to python was great not because they were going to use @ to multiply matrices but because now they could overload @ to do things with their own types. In Scala you wouldn't have to wait for a new operator to be added to the language - "defining a custom operator" is exactly the same as "overloading an existing operator". If you want to make "a + b" work, you define a "+" method on a. If you want to make "a @ b" work, you define an "@" method on a. If you want to make "a ə b" do something, you define a "ə" method. You're right that scala does special-case the precedence of a small number of operators (the list is much shorter than the likes of C, but I still wish we could move away from it).
- draegtun 13y agore: operator precedence Io uses Operator Shuffling to move those operator method calls back into normal mathematical expectation (or to be exact... C precedence order) prior to building the AST. Ruby's parser must do (or does) something similar. From lmm answer it sounds like Scala does something similar to Ruby here. Smalltalk, Self & Rebol have no operator precedence and simply interpret the operators from left to right (using parenthesis to enforce higher precedence). Some refs: - http://iolanguage.org/scm/io/docs/IoGuide.html#Syntax-Operators http://iolanguage.org/scm/io/docs/IoGuide.html#Syntax-Operat... - http://stackoverflow.com/a/2926639/12195 http://stackoverflow.com/a/2926639/12195 - http://www.rebol.com/docs/core23/rebolcore-11.html http://www.rebol.com/docs/core23/rebolcore-11.html
- zephyrfalcon 13y agoHm, in Python, 2 + 3 is just sugar for (2).__add__(3). Not that different; Python just doesn't allow symbols like '+' in method names.
- lmm 13y ago> in Python, 2 + 3 is just sugar for (2).__add__(3). Sure, but it doesn't work the other way; there's no nicer way to write 2.florble(3). So you have a few operator methods, and then other methods are second-class citizens. Which in turn encourages people to abuse the operators as shortcuts for commonly-used operations, á la C++'s <<
- klodolph 13y ago> Which in turn encourages people to abuse the operators... In practice, very few languages have suffered problems from operator overloading. I can't think of another language where this is a problem (other than C++), which makes me think it's a cultural/code style problem, rather than a problem with the language itself. C++ seems to encourage abuse because everyone learns its IO library, and its IO has an API design so horrible it should make you retch. By comparison, I'm not sure that adding more operators makes the resulting code any more palatable. Look at Haskell: libraries define their own operators, and the operators in Haskell have a steep learning curve. Can you tell me, without looking at a chart, whether $ or >>= has higher precedence, and what kind of associativity they have?
- lmm 13y ago> Can you tell me, without looking at a chart, whether $ or >>= has higher precedence, and what kind of associativity they have? No, which is why I don't think that should be allowed. Scala doesn't do that - all "custom operators" have the same precedence, and the global rule is that anything ending in : associates to the right (and is defined by the thing on its right), otherwise to the left. So I could answer those questions easily for a Scala library.
- 13y ago