
- 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 - Semicolon
A semicolon (;) is punctuation mark in programming used to separate different lines of code. There are many programming languages, i.e., C, C++, Java, Pascal etc. However, in modern languages like Python and Scala, you usually don not need to use semicolons.
A semicolon marks the end of a line. It indicates the end of an expression and statement. But in Scala, the end of a line is enough to indicate the end of an expression. So, you do not have to use semicolons.
In Scala, semicolon is optional, same as in the Python programming language.
Semicolon Inference
In many programming languages, you need to put a semicolon at the end of your statements. However, some languages like JavaScript, Go, Kotlin, Swift, Scala, and Lua allow you to skip the semicolon if you do not want to use it.
We will discuss here rules that these languages use to decide when a semicolon is needed and when it is not. It also presents arguments for and against uses of semicolons optional.
Newline Characters
Scala is a line-oriented language where statements may be terminated by semicolons (;) or newlines. A semicolon at the end of a statement is usually optional. You can type one if you want but you don't have to if the statement appears by itself on a single line. On the other hand, a semicolon is required if you write multiple statements on a single line. Below syntax is the usage of multiple statements.
val s = "hello"; println(s)
The Challenge
If every statement was short and could fit on a single line. Then adding a semicolon would be easy. The computer could automatically add a semicolon at the end of each line that contains a statement. But sometimes, statements are long and need to stretch over multiple lines.
One simple solution would be to use a special symbol to indicate when multiple lines belong to a single statement. This marker, like a backslash, could appear at the end of unfinished lines and at the start of continuation lines, like this.
printf("Solution results: %s %s %s\n", \ length, width, height) // The semicolon is added after ')'
However, this method has its problems. It is easy for a programmer to forget to put the continuation character, and it can make it harder for someone reading the code to understand the statement's logic.
Language designers often prefer to make semicolon handling smarter. It can automatically figure out when a statement continues across multiple lines. One useful method is to detect when a statement is not complete yet, and only add a semicolon when it's logically complete, like in these cases.
When a line ends with an operator that needs more input −
int foo = a + // No semicolon, as the '+' operator needs more input 2
But sometimes, it is not clear-cut. Sometimes, a statement is logically complete even though it is split across multiple lines, like this −
int foo = a - 2
Should this be considered one statement (int foo = a-2;) or two (int foo = a; -2;)? The first approach can lead to style guides suggesting that some statements should start with a semicolon to prevent them from accidentally merging with the previous line's statements. In either case, there is a risk of the compiler interpreting the code differently from what the programmer intended, but not signaling any potential problems.
Rules of Using Semicolon
In Scala, the rules for when to use semicolons are a bit different from some other languages. A line ending is considered a semicolon, unless −
- The line ends with a word that cannot legally conclude a statement, like a period and an infix operator.
- The next line starts with a word that cannot begin a statement on its own.
- The line ends inside parentheses (…) and brackets […], as these cannot contain multiple statements.
The first rule is like Go's rules, allowing continuation when the statement is incomplete. The second rule permits continuation when the next line cannot be a complete statement by itself. It handles situations where an infix operator either ends the previous line and starts the next one in most cases. However, if an infix operator can also be a valid prefix operator, it may cause issues.
Example
For example, consider this code −
let list2 = list1 // Semicolon added here |> myListFunction // ... and here |> myOtherListFunction
To make this better, they came up with a new set of rules. These rules say that if there are two words, 'a' and 'b,' on separate lines, and 'a' is in the "beforeSemicolon" group, and 'b' is in the "afterSemicolon" group, then a semicolon should be added. For example, a semicolon might be added if both words are names and if one line ends with ')' and the next starts with '('.
Regarding Scala's third rule, it means a statement can take up as many lines as it needs until all the open parentheses and brackets have been closed. This is similar to how Python works and helps prevent the problem shown in the earlier Go example.
Note - Note that you need to use semicolons when using postfix operators. This is one of the reasons why postfix operators are language features. You will be cautioned about if you haven't specifically enabled them.