January 2, 2021
Estimated Post Reading Time ~

Getting started with Scala

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 the command prompt or terminal and if everything is OK you will see a welcome message.
command prompt

Scala has REPL (Read-Eval-Print-Loop) which allows you to write Scala code and immediately see the results of your code in the command prompt/terminal. REPL allows you to experiment with all kinds of Scala expressions and immediately see results.
command prompt scala repl

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 a Scala worksheet and in them, you can experiment with your code.

Scala worksheet

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 a variable you need to choose if you want an immutable (val) or mutable (var) variable. Then you write a name, after the name you optionally write the type of a variable, and on the right side of equality, you assign some value, object, or expression. Defining type is optional because the compiler can figure out it from the 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 the return keyword to return some value from the method, it’s enough to write a 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. The 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 a method that doesn’t take an argument and has no side effects (pure functions) like mutating the state of objects or I/O operations then when you call the 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 a Scala class you need to write the keyword “class” and the 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 field 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 immutable then 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 the 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.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.