So you want to write your first Scala application? Before that, you need to install Scala on your PC and know some basic syntax.
First, you need to install Java JDK and Scala binaries. To check if you have successfully installed Scala just write command “scala” in command prompt or terminal and if everything is OK you will see a welcome message.
Scala has REPL (Read-Eval-Print-Loop) which allows you to write Scala code and immediately see results of your code in command prompt/terminal. REPL allows you to experiment with all kind of Scala expressions and immediately see results.
If you don’t like cmd/terminal you can use your favorite IDE or text editor. IntelliJ, Eclipse and NetBeans have good support for writing Scala code. Also if you want REPL in your IDE you have to create Scala worksheet and in them you can experiment with your code.
Scala syntax
Variable declarations
In Scala, you have two types of variables, immutable and mutable. Immutable variables are read-only and that means when you declare a variable, later you can’t assign new values/objects to that variable. An immutable variable is declared with keyword val.
val name: String = "Scala"
//shorter
val newName = "Scala"
// you can't assign new value
newName = "Java" // Error: reassignment to val name = "Java"
Mutable variables are declared with keyword var and later you can assign new values/objects to that variable.
var age = 23
//you can assign new values
age = 24
age = 25
So keywords val (immutable) and var (mutable) specify whether the reference can be changed to a different object (var) or not (val).
To declare variable you need to choose if you want immutable (val) or mutable (var) variable. Than you write name, after name you optionally write type of variable and on right side of equality you assign some value, object or expression. Defining type is optional because compiler can figure out it from right side of equality.
val nameOfImmutableVariable: type = value
var nameOfMutableVariable: type = value
Method declarations
Method declarations start with keyword def , followed by optional argument list, optional return type and after equals sign body of the method.
def methodName (argument: argumentType): returnType = {
//body of the method
}
Methods that don’t return some value for return type have a Unit type.
def printHelloWorld: Unit = {
println("Hello world")
}
Also, you don’t have to write return keyword to return some value from the method, it’s enough to write value or some object that you want to return.
def sum(a: Int, b: Int): Int = {
a + b
}
Scala methods can be written even more a concisely. Return type doesn’t have to be specified and parentheses around the body of the short method aren’t required.
def sum(a: Int, b: Int) = a + b
Scala is flexible when you working with methods. You can call object methods in two ways, the first way is to call a method on the object with dot character and the second way is like you write a sentence.
someobject.methodName()
//or
someobject methodName()
Also it’s important to mention when you call method which don’t take argument and has no side effects (pure functions) like mutating the state of objects or I/O operations than when you call method you don’t have to put rounded brackets after the method name.
List(1,2,3,4,5).size // 5
List(1,2,3,4,5).size() // error
Classes
To create Scala class you need to write keyword “class” and name of the class. A primary constructor of Scala class makes a combination of constructor parameters which are defined after class name and expressions and methods that are called in the body of the class. Depends on constructor parameters and fields types Scala automatically generates getter and setter methods. So if a field is mutable than are generated both getter and setter methods and if a field is an immutable than is generated the only getter method.
class Person(val firstName: String, val lastName: String){
println("beginning of the constructor")
var age = 25
def printInfo = println("Firstname: " + firstName + ", lastname: " + lastName + ", age: " + age)
printInfo
println("end of the constructor")
}
val person = new Person("Matija","Kovacek")
/*
beginning of the constructor
Firstname: Matija, lastname: Kovacek, age: 25
end of the constructor
*/
// getter
person.firstName
//setter
person.age = 30
No semicolons
You may have noticed that in previous examples I didn’t use semicolons. Scala is treating end of the line as the end of the statement or an expression so you don’t have to put semicolons. You only have to use semicolons when you want to put multiple statements or expressions on the same line.
Top comments (0)