An introduction to Scala
Tags: programming, javascript, algorithms
Published: 2022-02-27 00:53:47

Software development is an industry that is defined by rapid change, be it new tech, revived trends or an old idea that finally catches on to the mainstream. Scala may soon be an example of the latter of these. From the growing demand for typescript proficiency to smarter API handling with GraphQL, stronger typed technologies appear to be finding their way into historically less opinionated spaces. As a javascript developer who is currently learning typescript, I am interested in delving into other typed tech and landed on Scala, a language that was conceived of the criticisms of its predecessor, Java. So, in this blog I will briefly walk the reader through the basics of Scala and maybe provide some insight into what mainstream development could look like in the near future.

To begin we start by taking a look at how Scala deals with variables. Scala is an object-oriented language in that every value is an object, which means that every value can have a type. These types belong to a hierarchy, with the supertype "Any" at the top level. Any is used to define some universal methods that can apply to any value, such as toString or equals. The diagram below describes the shape of the hierarchy: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcfp5gor1ens6f4gsm3h.png)
 
We see above that the Any superclass has two immediate subclasses, AnyVal and AnyRef. In terms of javascript, we can think of the AnyVal class as simple data types and AnyRef as complex data types. AnyVal types are non-nullable and the Unit type is essentially a placeholder that can be returned from a function since all functions in Scala require some kind of return value. All non-value types are AnyRef types as well as any user-defined type. At the bottom of the hierarchy we have the "Nothing" type and Null. Nothing is a subtype of all types and has no value. It is typically used to signal that an expression will not yield a value or a method will not return normally. Null is a subtype of all reference types  and has the literal value of the keyword "null". Null is available in Scala as a way to work with Java code since Java does not have a Nothing subtype, and as such should not be used. 

Creating objects in Scala is done by defining classes that serve as a blueprint for object creation. Class names are capitalized and are instantiated with the keyword "new". Classes can contain values, objects, traits, variables, methods and other classes. Each of these properties defined on a class are called "members" of the class. Consider the below code: 

```
class Person(var name: String, var age: Int) {
  
  def talk(words : String){ //Return type is a string
    println(words)
}
# use the class
val zack = new Person("Zack", 35)
zack.name                        // "Zack"
zack.age                         // 35
zack.talk("Hello there!")        // "Hello there!"            
```
Here we have defined a class called Person that has three members: name, age and a method called talk. The variables are declared with the "var" keyword and are mutable. Had they been declared with the "val" keyword their value would not be able to be changed, similar to the const keyword in javascript. The talk method is a function that we declare with the "def" keyword and simply prints a line of text with the built in "println" method. Each member in the class has its values defined when they are declared and the method has its return value similarly defined beforehand. 

In sum, Java is a language that is strongly typed, and while Scala is also typed, its more flexible and hints toward an industry-wide shift toward the middle of the opinionated-unopinionated programming language spectrum. This shift is powered by a desire for code that is both expressive and less prone to bugs. One needs not be sacrifice for the other, and a future looms where the conversations surrounding what language works best in certain environments is going to get more nuanced. Why would we want it any other way?