Introduction
I started a new job, and I have to write scala. The last time I touched the language was two years ago, for about 2 weeks. My main takeaway then was disgust at the existance of something called the cake pattern.
This series is about me having to start embracing scala, coming from a primarily golang background.
Getting started
I started looking through my book repository, and found a book called Atomic scala. It goes through language features with example, more than the tour provides in my opinion.
Installing
It’s quite straight forward. The website’s instruction is easy to follow.
Implicit fields
class Human(val name:String, val age:Int)
This is equivalent of declaring fields and them assigning them in the constructor.
type Human struct {
name string
age int
}
func NewHuman(name string, age int) *Human {
return &Human{
name: name,
age: age,
}
}
Case classes
Organising information together is a common affair. In golang, one would create a struct. In scala, one would create a case class.
Printable
One benefit of using case classes is that it is amenable to being printed. ie, toString is automatically defined.
case class Foo(x: Int)
class FooClass(val x: Int)
val f = new Foo(1)
val g = new FooClass(1)
println(f) //Foo(1)
println(g) //my.package$FooClass@1cab0bfb
Pattern matching with destructing
case class Square(x: Int) extends Shape
case class Rectangle(x: Int, y:Int) extends Shape
def perimeter(s: Shape): Int = {
s match {
case Square(x) => x*4
case Rectangle(x, y) => 2*(x+y)
}
}
This can also be used for tuple.
This makes the code very terse and precise.
String interpolation
In golang, fmt.Printf
has been one of my favourite goto. Knowing some Java, I knew there was a System.out.println()
, but that doesn’t format things well, so I looked for printf equivalent, and found String.format()
. That was fine until I tried to print multiple arguments: String.format("%b %b", true, false)
and that died. Turns out there is a even simpler syntax which is "%b %b".format(true, true)
.
Functional approach and comprehension
Functional programming often encourages map, filter, etc. All these are missing from golang.
Scala introduces comprehensions which combines generators, filters and definitions.
val v = Range(0, 10)
for {
n <- v
if n < 5
if n %2 == 0
} yield {
n * 10
} // Vector(0, 20, 40)
Collections
Java collections allows the nice conversion between collections.
val v = Vector[Int](1,2,3)
val y = v.toList.toSet.toVector
Trolls
There are many syntactic sugar, and some of them can be rather confusing.
-> Is the same as a tuple.
So {(a -> b)}
is the same as {(a,b)}
. I suppose this is to make map more readable.
val z = Map{
"a" -> 1
}
Afterthoughts
- Scala is a lot of syntactic sugar on top of Java
- There is still too many ways to do one thing, comparing this with golang where there’s too few ways to do one thing.
- There are language features that wer not available in go: multiple constructors, default values, named values
- Scala tries to remove unnecessary syntax tokens. This means the mind needs to take some time to do manual pattern matching before understanding code.
- A cookbook can be rather useful. I probably also need to practice on katas.