Rules for Operators in Scala



Operator is a symbol that performs an operation to be carried out on one or more operands. Operators are essential in any programming language. In expressions with multiple operators, the order in which the operations are performed is determined by operator precedence.

For example, in the expression 5 + 8 * 2, the multiplication is performed first. So, it will be 5 + (8 * 2) rather than (5 + 8) * 2. Associativity decides the order of operations if two operators have the same precedence in an expression. Associativity can be either right-to-left or left-to-right. For example, the operators '+' and '-' have the same precedence and their associativity is left-to-right. Thus, the expression "20 - 5 + 3" is evaluated as "(20 - 5) + 3".

Operators Precedence

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator −

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Take a look at the following table. Operators with the highest precedence appear at the top of the table and those with the lowest precedence appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Category Operator Associativity
Postfix () [] Left to right
Unary ! ~ Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift >> >>> << Left to right
Relational > >= < <= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left
Comma , Left to right

Operators Associativity

Associativity is the order in which operators of the same precedence are evaluated. Most operators in Scala are left-associative, i.e., these evaluated from left to right. But there are also some operators which evaluate right to left like assignment operators.

For example: according to the associativity given in the above table.

  • Left-associative − x - y - z is evaluated as (x - y) - z.
  • Right-associative − x # y # z is evaluated as x # (y # z).

Operator Overloading

You can define custom behavior for an operator. It is known as Operator Overloading. You can define this method in your class that matches the name of the operator.

Example

case class Matrix(elements: List[List[Int]]) {
  def +(other: Matrix): Matrix = {
    val resultElements = (elements zip other.elements).map {
      case (row1, row2) => (row1 zip row2).map { case (a, b) => a + b }
    }
    Matrix(resultElements)
  }
}

val matrix1 = Matrix(List(List(1, 2), List(3, 4)))
val matrix2 = Matrix(List(List(5, 6), List(7, 8)))
val sum = matrix1 + matrix2 // Matrix(List(List(6, 8), List(10, 12)))

Here, we have overloaded + operator to perform matrix addition. So you can add two matrices easily.

Rules for Operator Overloading

There are some rules for operator overloading, these are given as follows −

  • You can overload only existing operators. You cannot overload new operators.
  • You cannot modify the number of operands.
  • You can change their precedence and associativity rules.
  • You can overload operators by defining methods with symbolic names in the class.
  • Operator methods must be defined as member functions of the class.
  • There must be at least one user-defined type operand.
  • You cannot overload = and compound assignments like +=
Advertisements