Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator precedence in Scala

I like Scala's propose of operator precedence but in some rare cases, unmodified rules may be inconvenient, because you have restrictions in naming your methods. Are there ways to define another rules for a class/file, etc. in Scala? If not, would it be resolved in the future?

like image 491
Jeriho Avatar asked May 27 '10 15:05

Jeriho


People also ask

What are the operators in Scala?

3.2. There are five relational operators in Scala: Greater than (>) Less than (<) Greater than or equal to (>=)

Which operator has highest precedence in */ ()?

The logical-AND operator ( && ) has higher precedence than the logical-OR operator ( || ), so q && r is grouped as an operand. Since the logical operators guarantee evaluation of operands from left to right, q && r is evaluated before s-- .

What does =!= Mean in Scala?

It has no special meaning whatsoever. It is also not a well-known method name in Scala. It seems to come from some library; you need to look at the documentation of whatever library you are using to figure out what it does.

What is operator overloading in Scala?

Operator Overloading in Scala Scala supports operator overloading, which means that the meaning of operators (such as * and + ) may be defined for arbitrary types. Example: a complex number class: class Complex(val real : Double, val imag : Double) { def +(other : Complex) = new Complex( real + other.


2 Answers

Operator precedence is fixed in the Scala Reference - 6.12.3 Infix Operations by the first character in the operator. Listed in increasing order of precedence:

(all letters) | ^ & = ! < > : + - * / % (all other special characters) 

And it's not very probable that it will change. It will probably create more problems than it fixes. If you're used the normal operator precedence changing it for one class will be quite confusing.

like image 71
Thomas Jung Avatar answered Sep 16 '22 15:09

Thomas Jung


Are there ways to define another rules for a class/file, etc. in Scala? If not, would it be resolved in the future?

There is no such ability and there is little likelihood of it being added in the forseeable future.

like image 40
Randall Schulz Avatar answered Sep 20 '22 15:09

Randall Schulz