One of the advantages of using a statically typed language is that you can use the type system to enforce
some constraints. Scala provides self-recursive types, aka F-bounded polymorphic types
that–along with self types–let you put powerful constraint to your type definitions.
Self-recursive type definition
Terminology apart, let me show you one of the use cases where this could be useful. Consider the following example
which does not use a self-recursive type:
This Domain-Specific Language (DSL) lets you perform operations among different currencies,
by transparently doing all internal conversions. The conversion map is injected implicitly by the client code.
Usage Example
Here’s a simple usage example:
importcom.lambdista.money._importcom.lambdista.money.Currency._objectMain {
def main(args:Array[String]):Unit = {
val conversion:Conversion = Map(
(GBP, EUR) -> 1.270,
(EUR, USD) -> 1.268,
(GBP, USD) -> 1.611 )
implicitval converter =Converter(conversion)
val sumAndConversion1 =100.001(USD) + 200(EUR) to GBP println(s"sumAndConversion1: $sumAndConversion1")
val sumAndConversion2:Money = 100(USD) + 210.4(EUR) to EUR println(s"sumAndConversion2: $sumAndConversion2")
val sum =100.001(USD) + 200(EUR)
val simpleConversion = sum(GBP)
println(s"simpleConversion: $simpleConversion")
val sumWithSimpleNumber =100(USD) + 23.560 println(s"sumWithSimpleNumber: $sumWithSimpleNumber")
val multiplicationWithSimpleNumber =100(USD) * 23 println(s"multiplicationWithSimpleNumber: $multiplicationWithSimpleNumber")
val usd =Currency("USD")
val multiplication =100(usd) * 23(EUR)
println(s"multiplication: $multiplication")
val divisionWithSimpleNumber =100(USD) / 23 println(s"divisionWithSimpleNumber: $divisionWithSimpleNumber")
val comparison =100(USD) > 90(EUR)
println(s"100 USD > 90 EUR? $comparison")
}
}
As you can see the client code just needs two simple imports and an implicit value of type Converter
in order to use the DSL. The operations shown in the previous code are only a few among the available ones.
Have a look at the Money class for a complete coverage.
This API is a Java implementation of Scala Try API,
originally implemented by the guys at Twitter and later added to the Scala Standard Library.
The Try type represents a computation that may fail. If the computation is successful returns
the value wrapped in a Try.Success otherwise returns the
java.lang.Exception wrapped in a Try.Failure.
To use Try you need to call the Try.apply(FailableSupplier) method passing in a lambda with
the same signature used for a common java.util.function.Supplier.
Indeed FailableSupplier is just a java.util.function.Supplier with a
throws Exception added to its get method.
The C programming language is the book he wrote along with Brian Kernighan. In my humble opinion, it is one of the best book ever written about computer programming.