Scala self-recursive types

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:

Scala DSL for currency-related operations

A simple internal DSL in Scala for money-related operations Source code: Scala DSL for money-related operations 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{defmain(args:Array[String]):Unit={valconversion:Conversion=Map((GBP,EUR)->1.270,(EUR,USD)->1.268,(GBP,USD)->1.611)implicitvalconverter=Converter(conversion)valsumAndConversion1=100.001(USD)+200(EUR)toGBPprintln(s"sumAndConversion1: $sumAndConversion1")valsumAndConversion2:Money=100(USD)+210.4(EUR)toEURprintln(s"sumAndConversion2: $sumAndConversion2")valsum=100.001(USD)+200(EUR)valsimpleConversion=sum(GBP)println(s"simpleConversion: $simpleConversion")valsumWithSimpleNumber=100(USD)+23.560println(s"sumWithSimpleNumber: $sumWithSimpleNumber")valmultiplicationWithSimpleNumber=100(USD)*23println(s"multiplicationWithSimpleNumber: $multiplicationWithSimpleNumber")valusd=Currency("USD")valmultiplication=100(usd)*23(EUR)println(s"multiplication: $multiplication")valdivisionWithSimpleNumber=100(USD)/23println(s"divisionWithSimpleNumber: $divisionWithSimpleNumber")valcomparison=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.

Try-Success-Failure API for Java 8

Try-Success-Failure API: Java implementation of the famous Scala counterpart Source code: Try-Success-Failure API for Java 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.