Patrick Premont of BoldRadius presented this talk at Scala By The Bay 2015.
Why do data structure lookups often return Options? Could we safely eliminate all the recovery code that we hope is never called? We will see how Scala’s type system lets us express referential integrity constraints to achieve unparalleled reliability. We apply the technique to in-memory data structures using the Total-Map library and consider how to extend the benefits to persisted data.
16. COVARIANT
ID TYPE PARAMETERS
case class Shipment[+P](
quantity: Int,
part: P)
val parts: Total[String]
val shipments:
List[Shipment[parts.Id]]
17. MULTIPLE CONSTRAINTS
case class Country(name: String)
case class Address[+CountryId](country: CountryId)
case class Customer[+AddressId](address: AddressId)
trait Data {
val countries : Total[Country]
val addresses : Total[Address[countries.Id]]
val customers : List[Customer[addresses.Id]]
}
19. BREAKING THE CYCLE
case class User[+U](
name: String,
follows: List[U])
trait Data {
val userSet : Total[Unit]
val users : userSet.Map[User[userSet.Id]]
}
20. COMPLEMENT TYPES
trait Total {
type Id
type Comp
def allocate: Comp
def insertAt(id: Comp) : Total {
type Id >: Total.this.Id
}
}
21. ID TYPE FAMILY
Nothing {}
Option[Nothing] {None}
Option[
Option[Nothing]]
{None,
Some(None)}
Option[
Option[
Option[Nothing]]]
{None,
Some(None),
Some(Some(None))}
25. PERFORMANCE
• Total maps are binary trees: O(log n) operations
• No rebalancing: identifiers designate fixed paths
to a node
• n is the highest index in use