DEV Community

Mircea Sirghi
Mircea Sirghi

Posted on

From Java to Scala Sequel

I am going to document here my migration from Java to Scala.

After around 20 days, I understood that Scala is not just a programming language, it actually creates mini compilers for each method, this is what makes it different and difficult as it allows for a more diverse programming style than Java. However I have started to feel its flavor.

Something I noticed today is that lazy in Scala can be used to add a similar to tail recursive paradigm to functions that are not tail recursive. This way the computation is delayed and can run as a single unit. And even more important is that without lazy it is impossible to partition data and run parallel computations as left operators are computed before right ones.

Main

main method is implemented as part of object not as part of class

TODO: explain

Use If statement when converting from one object to another
Use case(Pattern matching) when getting a sub object

Two different types of declaring function params

f(a: Int) - param a is evaluated before function call
f(a: => Int) - call-by-name param a is evaluated each time it is referenced inside method when method is executed

Dictionary:

Variance
---- Usage List[+A]
---- Means A is a covariant type of list
-------- Like List[Dog] is sub list of List[Animal]
---- Java: List<? extends Animal>
---- Usage List[A]
---- Means A is a nonvariant or invariant
-------- Like List[Dog] is not a sub list of List[Animal]
---- Java: List<Dog>
Variadic function
---- Accepts one or more arguments
---- Usage def apply[A](as: A*): List[A] =
---- Java: <A> List<A> apply(A... values)
Pattern matching:
---- Java: Constructor overloading
Data sharing:
---- Adding an element to a list returns a new list with the new element
---- with a reference to the old list for the rest of the elements
Companion object:
---- Has same name as data type
---- Contains functions to work with data type
---- Java: Impl pattern

Top comments (0)