Implicit Parameters in Scala
Because Scala compiler can infer types that are not explicitly declared, it also makes it possible to use implicit parameters.
Because Scala compiler can infer types that are not explicitly declared, it also makes it possible to use implicit parameters.
One of the arguments used to praise functional programming is that “it is easy to reason about”. In a simplistic sense, it is in fact easy to think that something takes an input and returns an output. But there is much more to that argument.
When we start thinking about how to represent data in Object Oriented programming, we usually think about a way to make it extensible, so that we can use some design principles that will allow us to make changes to the program without changing existing code. With functional programming, that can be a little bit trickier.
A quick answer to the question of the title of this post is: it allows us to use pattern matching when comparing its instances and it does not need the keyword new
in order be initialized. But why? How?
When dealing with generic polymorphism and type parameterization, we can set some limitation to which type a method can accept.
Scala, as a functional and an object-oriented language, can implement both subtype and generic polymorphism. The first one is usually associated with OO languages, and the second, with functional.
If a function is declared inside an object, is it a function or a method?
One of the first things we learn when we start writing Scala code is that var
is mutable and val
, immutable. And that we can use them whichever way we need. But no beginners tutorial says that we should not use var
s inside an object
.
The Scala evaluation method makes it more efficient to use immutable values, but it still give us the chance to easily deal with mutable data. It is possible to write a program following the Functional Programming paradigm while being able to rely on mutable variables. Thanks to that, it is reasonably easy to implement the negamax with alpha beta pruning algorithm, improving the performance of an unbeatable computer in a Tic Tac Toe game.
In dynamically typed languages, it is a trivial task to write functions that return something or nothing. In a statically typed language like Scala, something does not have the same type as nothing. It is possible to check if something exists and handle the exception, but this is not exactly a good way to solve that kind of problem. Having an Option
is.
The same way that expressions can be written in different ways, code written in Scala can also be evaluated in different ways.
The fact that Scala can be written as an Object Oriented or a Functional language gives us at least two different ways to accomplish the same thing. But it is not only that: common tasks, like defining a function/method, also can be done in different ways.
Clojure has a special way to guarantee the tail recursion optimization, with the macro recur
. In Scala, we can call the function by its name, but there is also a special notation to make sure that the optimization will happen.
After three months as a student apprentice at 8th Light, I’m starting over, now as a resident. And this is not something trivial. I moved from another city to Chicago because of the apprenticeship, and of course I really wanted to continue in the program as a resident. I couldn’t be happier with the project that I developed as a student and with the project that I started working on.