
- Scala - Home
- Scala - Overview
- Scala - Features
- Scala - Environment Setup
- Scala - Build Tool (SBT)
- Scala - REPL
- Scala - Dot & Dotty
- Scala - Basic Syntax
- Scala - Hello World Program
- Scala - Identifiers
- Scala - Keywords
- Scala - Comments
- Scala - Code Blocks
- Scala - Semicolon
- Scala - Constructs
- Scala - Expressions
- Scala - Input and Output
- Scala - Optional Braces
- Scala - Underscore (_)
- Data Types and Variables
- Scala - Data Types
- Scala - Type Bounds
- Scala - Context Bound
- Scala - Variances
- Scala - Type Hierarchy
- Scala - Variables
- Scala - Variable Scopes
- Scala - Literals
- Scala - Numeric Types
- Scala - Boolean Types
- Scala - Char Type
- Scala - Unit Types
- Scala - Strings
- Scala - Arrays
- Scala - Null Type
- Scala - Nothing
- Scala - Any Type
- Scala - AnyRef Type
- Scala - Unified Types
- Scala - Dates and Times
- Scala - Ranges
- Scala - Multidimensional Arrays
- Scala - WrappedArray
- Scala - StringBuilder
- Scala - String Interpolation
- Scala - StringContext
- Scala - Type Casting
- Scala var vs val
- Scala Operators
- Scala - Operators
- Scala - Rules for Operators
- Scala - Arithmetic Operators
- Scala - Relational Operators
- Scala - Logical Operators
- Scala - Bitwise Operators
- Scala - Assignment Operators
- Scala - Operators Precedence
- Scala - Symbolic Operators
- Scala - Range Operator
- Scala - String Concatenation Operator
- Scala Conditional Statements
- Scala - IF ELSE
- Scala - IF-ELSE-IF-ELSE Statement
- Scala - Nested IF-ELSE Statement
- Scala Loop Statements
- Scala - Loop Statements
- Scala - while Loop
- Scala - do-while Loop
- Scala - Nested Loops
- Scala - for Loop
- Scala - break Statement
- Scala - yield Keyword
- Scala Classes & Objects
- Scala - Classes & Objects
- Scala - Constructors
- Scala - Auxiliary Constructor
- Scala - Primary Constructor
- Scala - This Keyword
- Scala - Nested Classes
- Scala - Getters and Setters
- Scala - Object Private Fields
- Scala - Singleton Object
- Scala - Companion Objects
- Scala - Creating Executable Programs
- Scala - Stateful Object
- Scala - Enumerations
- Scala - Polymorphism
- Scala - Access Modifiers
- Scala - Apply Method
- Scala - Update Methods
- Scala - UnapplySeq Method
- Scala - Inheritance
- Scala - Extending a Class
- Scala - Method Overloading
- Scala - Method Overriding
- Scala - Generic Classes
- Scala - Generic Functions
- Scala - Superclass Construction
- Scala Methods & Functions
- Scala - Functions
- Scala - Main Methods
- Scala - Functions Call-by-Name
- Scala - Functions with Named Arguments
- Scala - Function with Variable Arguments
- Scala - Recursion Functions
- Scala - Default Parameter Values
- Scala - Functions without Parameters
- Scala - Implicit Parameters
- Scala - Higher-Order Functions
- Scala - Nested Functions
- Scala - Extension Methods
- Scala - Anonymous Functions
- Partially Applied Functions
- Scala - Lazy Val
- Scala - Pure Function
- Scala - Currying Functions
- Scala - Control Abstractions
- Scala - Corecursion
- Scala - Unfold
- Scala - Tail Recursion
- Scala - Infinite Sequences
- Scala - Dynamic Invocation
- Scala - Lambda Expressions
- Scala Collections
- Scala - Collections
- Mutable and Immutable Collections
- Scala - Lists
- Scala - Sets
- Scala - Maps
- Scala - TreeMap
- Scala - SortedMap
- Scala - Tuples
- Scala - Iterators
- Scala - Options
- Scala - Infinite Streams
- Scala - Parallel Collections
- Scala - Algebraic Data Types
- Scala Pattern Matching
- Scala - Pattern Matching
- Scala - Type Patterns
- Scala - Exception Handling
- Scala - Extractors
- Scala - Regular Expressions
- Scala Files I/O
- Scala - Files I/O
- Scala Advanced Concepts
- Scala - Closures
- Scala - Futures
- Scala - Promises
- Scala - Traits
- Scala - Trait Mixins
- Scala - Layered Traits
- Scala - Trait Linearization
- Scala - Sealed Traits
- Scala - Transparent Traits
- Scala - Literal Type Arithmetic
- Scala - Inline keyword
- Scala - Def, Var & Val
- Scala - Dropped Features
- Scala - BDD Testing
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