# Scala (programming language)

Paradigm Multi-paradigm: functional, object-oriented, imperative, concurrent Martin Odersky Programming Methods Laboratory of École Polytechnique Fédérale de Lausanne 2.11.8 / March 8, 2016; 6 years ago[1] static, strong, inferred, structural JVM, JavaScript[2] 3-clause BSD license[3] www.scala-lang.org Eiffel, Erlang, Haskell,[4] Java, Lisp,[5] Pizza,[6] Standard ML, OCaml, Scheme, Smalltalk, Oz Ceylon, Fantom, F#, Kotlin, Lasso, Red, Swift

Scala is a programming language for general software applications. Scala has full support for functional programming and a very strong static type system. This allows programs written in Scala to be very concise and thus smaller in size than other general-purpose programming languages.

## History

The language was created in the 2001-2004 period in the laboratory of the EPFL programming methods. It was the result of research aimed at the development of better language support for component software. The basis for the development of language were taken 2 ideas:

1. The programming language component software should be scalable in the sense that it should be possible using the same concepts to describe small and large parts. Therefore, attention has been focused on the mechanisms of abstraction, composition and decomposition instead of introducing a large number of primitives, which can be useful only at a single zoom level.

2. Scalable support for components can be provided by the programming language that unifies and generalizes object-oriented and functional programming. Some of the major technical innovations Scala - it is a concept representing the fusion of these paradigms. In statically typed languages, which include Scala, these paradigms have been almost completely separated.

Scala was released for General use on the JVM in January 2004 and on the platform .NET in June 2004. It is planned to continue work on the formalization of key aspects of language and the development of optimizations performed by the compiler.

## Object-oriented language

Scala is a pure object-oriented language in the sense that every value is an object. Data types and behaviors of objects are described by classes and traits. Class abstractions are extended by subclassing and by a flexible mixin-based composition mechanism to avoid the problems of multiple inheritance.

For example, addition x+y is interpreted as ${\displaystyle x.+(y)}$, i.e. as a method call + with an argument y and x as the target object. Another example: ${\displaystyle 1+2}$. This expression is interpreted as ${\displaystyle (1).+(2)}$. Parentheses around the numbers required, because the lexical analyzer splits the Scala expression into lexemes according to the principle of the longest possible match. Thus, the expression ${\displaystyle 1.+(2)}$ will split into the tokens 1.,+ and 2, because the token 1. long lexemes 1 and the first argument of the addition will be interpreted as a Double type instead of Int.

## Functional language

Every function is a value. The language provides a lightweight syntax for defining anonymous functions and Karimovich. Each construct returns a value. Pattern matching naturally extends to the processing of XML using regular expressions.

## Integration

Scala is designed to interoperate with leading platforms such as Java or C#. It shares the most basic operators, data types and control structures with these languages. For simplicity in the following we will compare only Java with Scala. But since Java and C# have much in common, the similarity to Java also applies to C#. In some things Scala even closer to C# than to Java. For example, in the treatment of generality (genericity).

Classes and objects in Scala can inherit from Java classes and implement Java interfaces. This allows you to use Scala code in the Java framework. For example, the Scala class could implement the interface java.util.EventListener. Instances of this class can be then notified about events from Java code.

## Language features

### Example 1

As a first example we take the standard Hello World program.

object HelloWorld
{
def main(args: Array[String])
{
println("Hello, world!")
}
}

The structure of this program consists of a main-method that receives the command line arguments as array of strings as parameters. The method body consists of a single method println, the argument which is passed "Hello, World!". The main method returns nothing, so there is no need to explicitly specify the return type. The word object is a class whose object can exist only in one copy that is usually called a singleton. Such designation immediately defines the HelloWorld class and an object of this class, also called HelloWorld. This object will be created on demand, the first time you use it. main is not declared static. This is because Scala has no static members (methods or fields of class). Instead of defining static members, the Scala programmer declares them as singleton objects.

### Objects

In Scala everything is an object, including numbers or functions. In this Scala differs from Java because Java splits a primitive type (e.g. boolean or int) and reference types, and there is no way to work with functions as with the value.

Numbers are the objects too. Since numbers are objects, they also have methods. Actually, the following arithmetic expression:

${\displaystyle 1+2*3/x}$


consists exclusively of method calls, and equivalent to following expression:

${\displaystyle (1).+(((2).*(3))./(x))}$


It also means that the symbols +, * – valid identifiers in Scala. The parentheses around the numbers necessary because the lexical analyzer Scala uses the longest matching rule for tokens. It splits the following expression

${\displaystyle 1.+(2)}$


On the tokens 1., + 2.. Token 1. are interpreted as the literal 1.0, the result is Double instead of Int. In other words, if we write the expression as

${\displaystyle (1).+(2)}$


no one will think that 1 is a Double.

In Scala, functions are also objects. However, in Scala you can pass a function as an argument, save as a variable or return from another function. This ability to manipulate functions as the ordinary variables is the cornerstone of a very interesting programming paradigm - functional programming.

### Classes

We already know that Scala is an object-oriented language and therefore it has such a concept as a class. Classes in Scala are declared using a syntax which is similar to the one used in Java. Difference from Java is that classes in Scala can have parameters. This can be illustrated by the following definition of a complex number.

class Complex(real:Double, imaginary:Double)
{
def re() = real
def im() = imaginary
}

This class takes two arguments, which are the real and imaginary part of a complex number. These arguments must be passed when you creating objects of class Complex: new Complex(1.5, 2.3). This class contains two methods re and im to get these two parts.

It should be noted that the type of the return value of these two methods is specified implicitly. It will automatically be detected by the compiler, which will consider the right part of these methods and decide that in both cases, the return type will be Double.

### Methods with no arguments

A small problem of the methods re and im is that in order to call them you need to specify a pair of empty brackets after the method name, as for example in the following example:

object ComplexNumbers
{
def main(args: Array[String])
{
val c = new Complex(1.2, 3.4)
println("imaginary part: " + c.im())
}
}

It would be better to be able to access the real and imaginary parts, as if they were normal fields, and we do not need to specify a pair of empty brackets. This is perfectly doable in Scala, we need simply define a method without arguments. Such methods differ from methods with zero arguments in that it is not necessary to write brackets after their name, neither in the definition nor in use. So, our Complex class can be rewritten as follows:

class Complex(real: Double, imaginary: Double)
{
def re = real
def im = imaginary
}

### Inherit and override

All classes in Scala inherit from the superclass. When the superclass is not specified, as for example in the previous Chapter in the example of the Complex, it will default to use the scala class.AnyRef.

You can also redefine (override) the inherited method in the superclass. In Scala there is a requirement to explicitly specify the override modifier, in order to avoid accidental overriding. In our example, the Complex class can be augmented by overriding the toString method from Object.

class Complex(real: Double, imaginary: Double)
{
def re = real
def im = imaginary
override def toString() = "" + re + (if (im < 0) "" else "+") + im + "i"
}

## References

1. https://ru.wikipedia.org/wiki/Scala