Scala - Symbolic Operators



The symbolic operators are special symbols in Scala that represent methods. These symbolic operators allow you to write code with more readable syntax by using characters like +, -, *, /, <, >, &&, ||.

These operators are used to perform operations without calling explicit method names.

There are four categories of symbolic operators. These are given below.

Keywords/Reserved Symbols

Some symbols in Scala are special keywords or reserved symbols, integral to the language's syntax. Below are some notable examples −

Keywords/Reserved Symbols

Some symbols in Scala are special keywords or reserved symbols, integral to the language's syntax. Below are some notable examples:

1. Keywords

  • <-: Used in for-comprehensions to separate pattern from generator.
  • =>: Used for function types, function literals, and import renaming.

2. Reserved Symbols

  • () − Delimit expressions and parameters.
  • [] − Delimit type parameters.
  • {} − Delimit blocks.
  • . − Method call and path separator.
  • /* */ − Comments.
  • # − Used in type notations.
  • : − Type ascription or context bounds.
  • < − > − <% − Upper, lower, and view bounds.
  • <? <! − Start token for various XML elements.
  • " """ − Strings.
  • ' − Indicate symbols and characters.
  • @ − Annotations and variable binding on pattern matching.
  • ` − Denote constant or enable arbitrary identifiers.
  • , − Parameter separator.
  • ; − Statement separator.
  • _ − Many different meanings, such as wildcard, anonymous function placeholder, eta expansion, and more.

Automatically Imported Methods

Scala automatically imports these methods. So these are available without explicit import statements. Every Scala code includes three automatic imports −

import _root_.java.lang._
import _root_.scala._
import _root_.scala.Predef._

These imports bring various classes, singleton objects, implicit conversions and methods into scope. For example, the -> operator is used for creating tuples. It is defined in the ArrowAssoc class via the any2ArrowAssoc implicit conversion method in Predef.

Common Methods

Symbols represent methods defined within classes. Consider the following examples −

Example 1 − The -> operator creates tuples.

val tuple = "a" -> 1

Example 2 − The :: method is used with lists.

val list = 1 :: List(2, 3)

Example 3 − The += method is found in buffer classes.

val buffer = scala.collection.mutable.Buffer(1, 2)
buffer += 3  // Appends 3 to the buffer

Methods ending in a colon : bind to the right. So, the type on the right side of the operator determines the method class.

Syntactic Sugars/Composition

Scala provides syntactic sugars for more clarity and concise code. These syntactic constructs hide underlying method calls:

1. Apply and Update

These methods apply and update array-like indexing and assignment.

class Example(arr: Array[Int] = Array.fill(5)(0)) {
  def apply(n: Int) = arr(n)
  def update(n: Int, v: Int) = arr(n) = v
}
val Ex = new Example
println(Ex(0))  // Calls apply(0)
Ex(0) = 2       // Calls update(0, 2)

2. Anonymous Functions

These symbols can represent anonymous functions.

val add = (_: Int) + (_: Int)
println(add(1, 2))  // Output: 3

3. Assignment-like Methods

These methods can be combined to form assignment-like syntax.

var x = 5
x += 3  // Equivalent to x = x + 3
println(x)  // Output: 8

Example

object Demo {
  def main(args: Array[String]) = {
    val tuple = "Scala" -> "Powerful"
    println(s"Tuple: $tuple")

    val list = 1 :: List(2, 3)
    println(s"List: $list")

    val buffer = scala.collection.mutable.Buffer(1, 2)
    buffer += 3
    println(s"Buffer: $buffer")

    val add = (_: Int) + (_: Int)
    println(s"Add: ${add(4, 5)}")
  }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

\>scalac Demo.scala
\>scala Demo

Output

Tuple: (Scala,Powerful)
List: List(1, 2, 3)
Buffer: ArrayBuffer(1, 2, 3)
Add: 9
Advertisements