Scala
Scala is a flexible programming language that uses the Java Virtual Machine (JVM). It combines object-oriented and functional programming paradigms, which can be useful when a project benefits from both styles. Scala supports concurrent programming, although an application’s performance and suitability depend on its design and workload.
Getting Started with Scala
To begin using Scala, choose a toolchain and editor or IDE that support the Scala version used by your project. The Scala documentation discusses build tools such as sbt and Mill, while editor support depends on the toolchain. The examples below use standard Scala syntax and are intended to be tried in a matching REPL or project. Scala also interoperates with Java, so a JVM project can use both languages.
Scala Worksheets
Scala worksheets are a useful tool for quick testing and debugging. They show the output directly within an IDE that supports Scala worksheets, such as IntelliJ IDEA, making it easier to see the results of your code. Here’s a small object-initialization example for a worksheet, not a complete standalone program:
object Demo {
println("Hello Scala!")
}
Additionally, Scala provides a Read-Eval-Print Loop (REPL) similar to Python and other languages, allowing you to interactively test code snippets.
Variables in Scala
In Scala, you declare variables using var and val. The var keyword defines a mutable variable, while val creates an immutable binding that cannot be reassigned. An immutable binding is not the same as an immutable object: the object referenced by a val may still be mutable.
var i = 9
var j: Int = 10 // No semicolons required, similar to Python
val num: Int = 10 // The binding cannot be reassigned, similar to final in Java
Scala encourages immutability, which means most data should be declared with val.
Operators in Scala
Interestingly, operators in Scala are treated as functions. For example, the + operator is a method that can be invoked on numbers:
8 + 7 // This is equivalent to 8.+(7)
Scala lets methods have operator-like names, so an expression such as a + b can call a + method. Java does not provide user-defined operator overloading; it uses named methods for this kind of behavior instead.
Working with Classes in Scala
Defining classes in Scala is straightforward. You can create a simple class with a single line:
case class Student(var rollNo: Int, var name: String = "Sujal", var marks: Int = 10)
This line not only defines a class but also creates a constructor. Scala also supports constructor overloading and methods with operator-like names. Java uses named methods for user-defined behavior rather than Scala’s operator syntax.
var s1: Student = Student(10, name = "SujalC")
Creating methods inside the class is also simple:
case class Student(var rollNo: Int, var name: String = "Sujal", var marks: Int = 10) {
def show() = print("hi")
def display() = {
println("Great Scala")
}
def >(s2: Student): Boolean = marks > s2.marks
}
Lists and Lambda Expressions
Scala provides powerful collections like List and supports lambda expressions. Here’s how you can work with lists:
var nums: List[Int] = List(2, 3, 4, 5)
for (n <- nums) {
println(n)
}
You can use lambda expressions to perform operations on the list:
nums.foreach { i: Int => println(i) }
Scala’s List is immutable by default, and operations like reverse return a new list:
nums = nums.reverse
Chaining Functions
Scala allows you to chain functions together, creating expressive and concise code:
nums.drop(2).take(2) // Returns the 3rd and 4th elements
nums drop 2 take 2 // This also works without dots or parentheses
Working with Different Data Types
Scala’s AnyVal represents value types, while AnyRef represents reference types; on the JVM, AnyRef corresponds to java.lang.Object. You can create a List[Any] containing values of different types:
val arr: List[Any] = List(2, 3, true, "Yes!")
Tuples in Scala
Tuples allow you to group multiple values into a single compound value. Here’s an example of how to use them:
case class Student(rollNo: Int, name: String, marks: Int)
val students = List(
Student(1, "Asha", 82),
Student(2, "Ravi", 54)
)
val (passed, failed) = students.partition(s => s.marks >= 60)
Tuples are a powerful way to work with grouped data in Scala.
Conclusion
Scala is a sophisticated language that combines functional and object-oriented programming. Its syntax and features, like operator overloading, REPL, and immutable collections, make it a useful option for developers building concurrent applications. Scala’s tools and Java interoperability can be useful to Java developers, depending on the project and their experience with Scala.