4 ms·
I don't understand it either. It's just yet another Java limitation that better languages simply don't have and which must be constantly worked around. There's
by matheusmoreira 5y ago
I don't understand it either. It's just yet another Java limitation that better languages simply don't have and which must be constantly worked around. There's no reason why we can't have a method named + which adds the receiver to the argument. It's just a simple symbol.
x + y
x.+(y)
The root cause of this is the distinction between primitive and object types. Simply put, not everything is an object in Java and this causes numberless problems. One of them is the need for special syntax cases for the mathematical operators of the primitive types.
- dtech 5y agoThis is exactly how scala does it
- matheusmoreira 5y agoYeah. It's also how Ruby and Smalltalk do it. Lisp also does similar things. In these languages it doesn't even make sense to talk about operators. They don't exist. There are only methods, functions. General ideas represented by a few universal concepts. No special cases. Of course they can be overridden... It would have been strange if they couldn't. They're just normal methods or functions.
- igouy 5y agoNo special cases; apart from the special cases ;-) Some Smalltalk methods are "compiled in-line" special — ifTrue: alternativeBlock "If the receiver is false (i.e., the condition is false), then the value is the false alternative, which is nil. Otherwise answer the result of evaluating the argument, alternativeBlock. Create an error notification if the receiver is nonBoolean. Execution does not actually reach here because the expression is compiled in-line." self subclassResponsibility
- matheusmoreira 5y agoThat's just the implementation though, isn't it? In the language proper, the method still exists. The implementation just compiles it away.
- igouy 5y agoHow would — "Of course they can be overridden..." — work when the method is: ifTrue: