Scala Tutorial

Basics

Control Statements

OOP Concepts

Parameterized - Type

Exceptions

Scala Annotation

Methods

String

Scala Packages

Scala Trait

Collections

Scala Options

Miscellaneous Topics

Operators in Scala

In Scala, operators are not special language constructs, but rather they are method calls. Essentially, every operator in Scala corresponds to a method. Here's a closer look at Scala's approach to operators:

1. Infix Notation:

In Scala, methods with one parameter can be called using infix notation. For example:

val sum = 1 + 2

This is syntactic sugar for:

val sum = 1.+(2)

Here, + is a method of the Int class.

2. Arithmetic Operators:

The usual arithmetic operators are available:

  • + - addition
  • - - subtraction
  • * - multiplication
  • / - division
  • % - modulo

3. Relational Operators:

These operators compare two values:

  • == - equal to
  • != - not equal to
  • > - greater than
  • < - less than
  • >= - greater than or equal to
  • <= - less than or equal to

4. Logical Operators:

Used for boolean operations:

  • && - logical AND
  • || - logical OR
  • ! - logical NOT

5. Assignment Operators:

  • = - assign
  • += - increment and assign
  • -= - decrement and assign
  • *= - multiply and assign
  • /= - divide and assign
  • %= - modulo and assign

6. Unary Operators:

Prefix and postfix unary operators:

  • + - unary plus
  • - - unary minus
  • ! - logical NOT
  • ~ - bitwise NOT

For custom classes, you can define methods like unary_! to support custom unary operators.

7. Special Operators:

Scala allows defining custom operators. Any method can act as an operator. For example, in collections:

  • :: - list cons
  • ++ - concatenation

8. Bitwise Operators:

  • & - bitwise AND
  • | - bitwise OR
  • ^ - bitwise XOR
  • << - left shift
  • >> - right shift
  • >>> - unsigned right shift

9. Object Equality:

For reference equality:

  • eq - checks if two references are the same
  • ne - checks if two references are different

Recommendations:

  1. Although you can define custom operators, try to use them judiciously to maintain readability.
  2. Familiarize yourself with commonly used operators in libraries like Cats or Scalaz if you venture into functional programming in Scala. These libraries introduce a plethora of custom operators that might look foreign at first but can be highly expressive.

Remember, Scala's approach to operators empowers developers to create DSLs (Domain Specific Languages) and expressive APIs. However, it's essential to balance expressiveness with clarity and readability.

  1. List of operators in Scala:

    • Description: Operators in Scala are symbols or keywords that perform operations on operands.
    • Code Example:
      val sum: Int = 5 + 3
      
  2. Arithmetic operators in Scala:

    • Description: Arithmetic operators perform basic mathematical operations.
    • Code Example:
      val addition: Int = 5 + 3
      val subtraction: Int = 5 - 3
      val multiplication: Int = 5 * 3
      val division: Double = 5.0 / 3
      
  3. Comparison operators in Scala:

    • Description: Comparison operators compare values and return a Boolean result.
    • Code Example:
      val isEqual: Boolean = 5 == 3
      val isNotEqual: Boolean = 5 != 3
      val isGreater: Boolean = 5 > 3
      
  4. Logical operators in Scala:

    • Description: Logical operators perform logical operations on Boolean values.
    • Code Example:
      val andResult: Boolean = true && false
      val orResult: Boolean = true || false
      val notResult: Boolean = !true
      
  5. Bitwise operators in Scala:

    • Description: Bitwise operators perform operations at the bit level.
    • Code Example:
      val bitwiseAnd: Int = 5 & 3
      val bitwiseOr: Int = 5 | 3
      val bitwiseXor: Int = 5 ^ 3
      
  6. Unary operators in Scala:

    • Description: Unary operators operate on a single operand.
    • Code Example:
      val negativeValue: Int = -5
      val positiveValue: Int = +5
      val notValue: Boolean = !true
      
  7. Operator overloading in Scala:

    • Description: Operator overloading allows defining custom behaviors for standard operators.
    • Code Example:
      class Complex(val real: Double, val imaginary: Double) {
        def +(other: Complex): Complex = new Complex(real + other.real, imaginary + other.imaginary)
      }
      
      val complex1 = new Complex(1.0, 2.0)
      val complex2 = new Complex(3.0, 4.0)
      val result = complex1 + complex2
      
  8. Custom operators in Scala:

    • Description: Scala allows creating custom methods that resemble operators.
    • Code Example:
      class Circle(val radius: Double) {
        def *(factor: Double): Circle = new Circle(radius * factor)
      }
      
      val circle = new Circle(5.0)
      val scaledCircle = circle * 2.0
      
  9. Precedence and associativity of operators in Scala:

    • Description: Precedence defines the order in which operators are evaluated, and associativity defines the direction of evaluation.
    • Code Example:
      val result = 2 + 3 * 4 // Precedence: * has higher precedence than +