Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Academese to English: A Practical Tour of Scala...

Academese to English: A Practical Tour of Scala’s Type System

Scala is famous in part for having one of the richest type systems of all mainstream programming languages today. Despite its reputation, Scala’s type system remains one of the most under-documented and jargon-heavy aspects of Scala.

This talk will turn the academese into English, providing an example-rich tour of Scala’s type system, covering all the things that make people call it “powerful”. This talk isn’t about showcasing a bunch of challenging little logical puzzles with types; on the contrary, this talk is about showing practical uses of Scala’s type system, making it work for you and your users. We’ll see how we can use it to improve usability by reducing boilerplate, meanwhile keeping code type-safe. We’ll touch on the practical parts of Scala’s type system, all through examples.

Big thanks to:
Julien Richard-Foy, who provided a great co/contravariance example
http://julien.richard-foy.fr/blog/2013/02/21/be-friend-with-covariance-and-contravariance/
Rex Kerr and mucaho for a variance vs bounds SO example http://stackoverflow.com/questions/4531455/whats-the-difference-between-ab-and-b-in-scala
Bill Venners, for his explorations with type parameters vs type members http://www.artima.com/weblogs/viewpost.jsp?thread=270195
And David R. MacIver for a great running existentials example
http://www.drmaciver.com/2008/03/existential-types-in-scala/

Heather Miller

April 11, 2016
Tweet

More Decks by Heather Miller

Other Decks in Programming

Transcript

  1. A Practical Tour of Scala’s Type System Academese to English:

    Heather Miller @heathercmiller PhillyETE, April 11th, 2016
  2. Motivationfor this talk: You can do a ton of stuff

    with it. Let’s only look at stuff that 80% of people can rapidly apply. Scala’s got a very rich type system Yet, the basics could really be better explained. rule:
  3. Who is this talk for? Everyone. …except Scala type system

    experts. To show you some of the basics of Scala’s type system. Just the handful of concepts you should know to be proficient. My goal: Nothing fancy.
  4. Topics we’ll cover: e.g., Type-level programming, Higher Kinded Types, Path-Dependent

    Types, …, Dotty. Scala’s basic pre-defined types Defining your own types Parameterized types Bounds Variance Abstract types Existential types Type classes There’s a list of other stuff this talk won’t cover.
  5. Basic predefined types Scala’s java.lang String scala Boolean scala Iterable

    scala Any scala AnyVal scala Unit scala Double scala Float scala Char scala Long scala Int scala Short scala Byte scala Nothing scala Seq scala List scala Null scala AnyRef java.lang.Object ... (other Scala classes) ... ... (other Java classes) ... Implicit Conversion Subtype
  6. Basic predefined types Scala’s java.lang String scala Boolean scala Iterable

    scala Any scala AnyVal scala Unit scala Double scala Float scala Char scala Long scala Int scala Short scala Byte scala Nothing scala Seq scala List scala Null scala AnyRef java.lang.Object ... (other Scala classes) ... ... (other Java classes) ... Implicit Conversion Subtype
  7. Basic predefined types Scala’s java.lang String scala Boolean scala Iterable

    scala Any scala AnyVal scala Unit scala Double scala Float scala Char scala Long scala Int scala Short scala Byte scala Nothing scala Seq scala List scala Null scala AnyRef java.lang.Object ... (other Scala classes) ... ... (other Java classes) ... Implicit Conversion Subtype
  8. Define our own types? How do we Two ways: Define

    a class or a trait Declarations of named types e.g., traits or classes Define a type member using the type keyword 1.) class Animal(age: Int) { // fields and methods here... } trait Collection { type T }
  9. Define our own types? How do we Two ways: Define

    a class or a trait Declarations of named types e.g., traits or classes Define a type member using the type keyword 1.) Combine. Express types (not named) by combining existing types. 2.) e.g., compound type, refined type def cloneAndReset(obj: Cloneable with Resetable): Cloneable = { //... }
  10. Parameterized Types Interacting with typechecking via Same as generic types

    in Java. A generic type is a generic class or interface that is parameterized over types. What are they? class Stack[T] { var elems: List[T] = Nil def push(x: T) { elems = x :: elems } def top: T = elems.head def pop() { elems = elems.tail } } for example:
  11. Parameterized Types Interacting with typechecking via Same as generic types

    in Java. A generic type is a generic class or interface that is parameterized over types. What are they? class Stack[T] { var elems: List[T] = Nil def push(x: T) { elems = x :: elems } def top: T = elems.head def pop() { elems = elems.tail } } for example: Can interact with type- checking by adding or relaxing constraints on the type parameters using variance bounds
  12. Bounds? Both type parameters and type members can have type

    bounds: lower bounds (subtype bounds) upper bounds (supertype restrictions) Parameterized types; you can constrain them. Remember the type hierarchy? All types have an upper bound of Any and a lower bound of Nothing trait Box[T <: Tool] for example: trait Generic[T >: Null] { // `null` allowed due to lower // bound private var fld: T = null }
  13. for example: trait Generic[T >: Null] { // `null` allowed

    due to lower // bound private var fld: T = null } Bounds? Both type parameters and type members can have type bounds: lower bounds (subtype bounds) upper bounds (supertype restrictions) Parameterized types; you can constrain them. Remember the type hierarchy? All types have an upper bound of Any and a lower bound of Nothing trait Box[T <: Tool] A Box can contain any element T which is a subtype of Tool.
  14. Bounds? Both type parameters and type members can have type

    bounds: lower bounds (subtype bounds) upper bounds (supertype restrictions) Parameterized types; you can constrain them. Remember the type hierarchy? All types have an upper bound of Any and a lower bound of Nothing trait Box[T <: Tool] for example: trait Generic[T >: Null] { // `null` allowed due to lower // bound private var fld: T = null } Null can be used as a bottom type for any value that is nullable.
  15. Bounds? Both type parameters and type members can have type

    bounds: lower bounds (subtype bounds) upper bounds (supertype restrictions) Parameterized types; you can constrain them. Remember the type hierarchy? All types have an upper bound of Any and a lower bound of Nothing trait Box[T <: Tool] for example: trait Generic[T >: Null] { // `null` allowed due to lower // bound private var fld: T = null } Null can be used as a bottom type for any value that is nullable. Recall class Null from the type hierarchy. It is the type of the null reference; it is a subclass of every reference class (i.e., every class that itself inherits from AnyRef). Null is not compatible with value types. scala> val i: Int = null <console>:4: error: type mismatch; found : Null(null) required: Int
  16. Variance? How might they relate to one another? trait Box[T]

    class Tool class Hammer extends Tool Tool Hammer Box[Tool] Box[Hammer] Tool Hammer Box[Tool] Box[Hammer] Tool Hammer Box[Tool] Box[Hammer] Three possibilities: Given the following: Covariant Contravariant Invariant Parameterized types; you can constrain them.
  17. Covariance trait Animal class Mammal extends Animal class Zebra extends

    Mammal Let’s look at a simple zoo-inspired example. Given: We’d like to define a field for our animals to live on: abstract class Field[A] { def get: A } Now, let’s define a function isLargeEnough that takes a Field[Mammal] and tests if the field is large enough for the mammal to live in def isLargeEnough(run: Field[Mammal]): Boolean = … Can we pass zebras to this function? A Zebra is a Mammal, right? http://julien.richard-foy.fr/blog/2013/02/21/be-friend-with-covariance-and-contravariance/
  18. Covariance Nope. Field[Zebra] is not a subtype of Field[Mammal]. Why?

    Field, as defined is invariant. There is no relationship between Field[Zebra] and Field[Mammal]. scala> isLargeEnough(zebraRun) <console>:14: error: type mismatch; found : Run[Zebra] required: Run[Mammal] So let’s make it covariant! http://julien.richard-foy.fr/blog/2013/02/21/be-friend-with-covariance-and-contravariance/ abstract class Field[+A] { def run: A } Et voilà, it compiles.
  19. Contravariance Keeping with our zoo-inspired example, let’s say our zoo

    has several vets. Some specialized for specific species. We need just one vet to treat all the mammals of our zoo: http://julien.richard-foy.fr/blog/2013/02/21/be-friend-with-covariance-and-contravariance/ abstract class Vet[A] { def treat(a: A) } def treatMammals(vet: Vet[Mammal]) { … } Can we pass a vet of animals to treatMammals? A Mammal is an Animal, so if you have a vet that can treat animals, it will be OK to pass a mammal, right?
  20. Contravariance Nope. This doesn’t work because Vet[Animal] is not a

    subtype of Vet[Mammal], despite Mammal being a subtype of Animal. scala> treatMammals(animalVet) <console>:14: error: type mismatch; found : Vet[Animal] required: Vet[Mammal] We want Vet[A] to be a subtype of Vet[B] if B is a subtype of A. abstract class Vet[-A] { def treat(a: A) } So let’s make it contravariant! http://julien.richard-foy.fr/blog/2013/02/21/be-friend-with-covariance-and-contravariance/ Et voilà, it compiles.
  21. Wait, what’s the difference between A<:B and +B? http://stackoverflow.com/questions/4531455/whats-the-difference-between-ab-and-b-in-scala They

    seem kind of similar, right? Coll[A<:B] means that class Coll can take any class A that is a subclass of B. Coll[+B] means that Coll can take any class, but if A is a subclass of B, then Coll[A] is considered to be a subclass of Coll[B]. They’re different!
  22. Wait, what’s the difference between A<:B and +B? http://stackoverflow.com/questions/4531455/whats-the-difference-between-ab-and-b-in-scala They

    seem kind of similar, right? Coll[A<:B] means that class Coll can take any class A that is a subclass of B. Coll[+B] means that Coll can take any class, but if A is a subclass of B, then Coll[A] is considered to be a subclass of Coll[B]. They’re different! Useful when you want to be generic but require a certain set of methods in B Useful when you want to make collections that behave the same way as the original classes
  23. Wait, what’s the difference between A<:B and +B? http://stackoverflow.com/questions/4531455/whats-the-difference-between-ab-and-b-in-scala They

    seem kind of similar, right? Coll[A<:B] means that class Coll can take any class A that is a subclass of B. Coll[+B] means that Coll can take any class, but if A is a subclass of B, then Coll[A] is considered to be a subclass of Coll[B]. They’re different! Said another way… Given: class Animal class Dog extends Animal class Car class SportsCar extends Car variance: case class List[+B](elements: B*) {} // simplification val animals: List[Animal] = List( new Dog(), new Animal() ) val cars: List[Car] = List ( new Car(), new SportsCar() ) As you can see List does not care whether it contains Animals or Cars. The developers of List did not enforce that e.g. only Cars can go inside Lists.
  24. Wait, what’s the difference between A<:B and +B? http://stackoverflow.com/questions/4531455/whats-the-difference-between-ab-and-b-in-scala They

    seem kind of similar, right? Coll[A<:B] means that class Coll can take any class A that is a subclass of B. Coll[+B] means that Coll can take any class, but if A is a subclass of B, then Coll[A] is considered to be a subclass of Coll[B]. They’re different! Said another way… Given: class Animal class Dog extends Animal class Car class SportsCar extends Car Bounds: As you can see Barn is a collection only intended for Animals. No cars allowed in here. case class Barn[A <: Animal](animals: A*) {} val animalBarn: Barn[Animal] = Barn( new Dog(), new Animal() ) val carBarn = Barn( new SportsCar() ) // error: inferred type arguments [SportsCar] do not conform to method // apply's type parameter bounds [A <: Animal] // val carBarn = Barn(new SportsCar()) ^
  25. If you’re a Java developer, A lot of these things

    exist for Java. this may not be surprising. So how is this richer? Let’s look at some other aspects of Scala’s type system!
  26. Abstract type members A type member (member of an object

    or class) that is left abstract. Basic idea: Why is this desirable? Turns out that this is a powerful method of abstraction. Using abstract type members, we can do a lot of what parameterization does, but is often more flexible/ elegant! fundamental idea: Define a type and leave it “abstract” until you know what type it will be when you need to make it concrete in a subclass.
  27. Abstract type members fundamental idea: Define a type and leave

    it “abstract” until you know what type it will be when you need to make it concrete in a subclass. Example: trait Pet class Cat extends Pet Given: Let’s create a person, Susan, who has a Cat both using abstract type members and parameterization.
  28. Abstract type members fundamental idea: Define a type and leave

    it “abstract” until you know what type it will be when you need to make it concrete in a subclass. Example: class Person[Pet] class Susan extends Person[Cat] trait Pet class Cat extends Pet class Person { type Pet } class Susan extends Person { type Pet = Cat } Given: Abstract type members Parameterization
  29. Abstract type members http://www.artima.com/weblogs/viewpost.jsp?thread=270195 trait FixtureSuite[F] { // ... }

    trait StringBuilderFixture { this: FixtureSuite[StringBuilder] => // ... } class MySuite extends FixtureSuite[StringBuilder] with StringBuilderFixture { // ... } trait FixtureSuite { type F // ... } trait StringBuilderFixture { this: FixtureSuite => type F = StringBuilder // ... } class MySuite extends FixtureSuite with StringBuilderFixture { // ... } A bigger example from ScalaTest: Abstract type members Parameterization
  30. Abstract type members http://www.artima.com/weblogs/viewpost.jsp?thread=270195 trait FixtureSuite[F] { // ... }

    trait StringBuilderFixture { this: FixtureSuite[StringBuilder] => // ... } class MySuite extends FixtureSuite[StringBuilder] with StringBuilderFixture { // ... } trait FixtureSuite { type F // ... } trait StringBuilderFixture { this: FixtureSuite => type F = StringBuilder // ... } class MySuite extends FixtureSuite with StringBuilderFixture { // ... } A bigger example from ScalaTest: Abstract type members Parameterization The take away:
  31. Abstract type members http://www.artima.com/weblogs/viewpost.jsp?thread=270195 trait FixtureSuite[F] { // ... }

    trait StringBuilderFixture { this: FixtureSuite[StringBuilder] => // ... } class MySuite extends FixtureSuite[StringBuilder] with StringBuilderFixture { // ... } trait FixtureSuite { type F // ... } trait StringBuilderFixture { this: FixtureSuite => type F = StringBuilder // ... } class MySuite extends FixtureSuite with StringBuilderFixture { // ... } A bigger example from ScalaTest: Abstract type members Parameterization Abstraction without the verbosity of type parameters. (Can be DRYer). The take away:
  32. Existential types Intuitively, an existential type is a type with

    some unknown parts in it. Basic idea: Wombit[T] forSome { type T } For example, in the above, T is a type we don’t know concretely, but that we know exists. An existential type includes references to 
 abstract type/value members that we know exist, but whose concrete types/values we don’t know. Importantly,
  33. Existential types Intuitively, an existential type is a type with

    some unknown parts in it. Basic idea: Wombit[T] forSome { type T } For example, in the above, T is a type we don’t know concretely, but that we know exists. An existential type includes references to 
 abstract type/value members that we know exist, but whose concrete types/values we don’t know. Importantly, fundamental idea: Can leave some parts of your program unknown, and still typecheck it with different implementations for those unknown parts.
  34. Existential types Example: fundamental idea: Can leave some parts of

    your program unknown, and still typecheck it with different implementations for those unknown parts. case class Fruit[T](val weight: Int, val tooRipe: T => Boolean) class Farm { val fruit = new ArrayBuffer[Fruit[T] forSome { type T }] } Note that existentials are safe, whereas Java’s raw types are not.
  35. Existential types Let’s look at another example. http://www.drmaciver.com/2008/03/existential-types-in-scala/ scala> def

    foo(x: Array[Any]) = println(x.length) foo: (Array[Any])Unit scala> foo(Array("foo", "bar", "baz"))
  36. Existential types Let’s look at another example. http://www.drmaciver.com/2008/03/existential-types-in-scala/ scala> def

    foo(x: Array[Any]) = println(x.length) foo: (Array[Any])Unit scala> foo(Array("foo", "bar", "baz")) This doesn’t compile, because an Array[String] is not an Array[Any]. However, it’s completely typesafe–we’ve only used methods that would work for any Array. How do we fix this? :6: error: type mismatch; found : Array[String] required: Array[Any] foo(Array[String]("foo", "bar", "baz"))
  37. Existential types Attempt #2: Type parameters http://www.drmaciver.com/2008/03/existential-types-in-scala/ scala> def foo[T](x:

    Array[T]) = println(x.length) foo: [T](Array[T])Unit scala> foo(Array("foo", "bar", "baz")) 3 Now foo is parameterized to accept any T. But now we have to carry around this type parameter, and we know we only care about methods on Array and not what the Array contains. So it’s really not necessary. We can use existentials to get around this.
  38. Existential types Attempt #3: Existentials http://www.drmaciver.com/2008/03/existential-types-in-scala/ scala> def foo(x: Array[T]

    forSome { type T}) = println(x.length) foo: (Array[T] forSome { type T })Unit scala> foo(Array("foo", "bar", "baz")) 3 Woohoo! Note that a commonly-used shorthand is: Array[_] Existential types provide a way of abstracting type information, such that (a) a provider can hide a concrete type ("pack"), and thus avoid any possibility of the client depending on it, and (b) a client can manipulate said type by only by giving it a name ("unpack") and making use of its bounds. Existentials play a big role in our understanding of abstract data types and encapsulation. - Burak Emir
  39. Existential types Attempt #3: Existentials http://www.drmaciver.com/2008/03/existential-types-in-scala/ scala> def foo(x: Array[T]

    forSome { type T}) = println(x.length) foo: (Array[T] forSome { type T })Unit scala> foo(Array("foo", "bar", "baz")) 3 Woohoo! Note that a commonly-used shorthand is: Array[_] Existential types provide a way of abstracting type information, such that (a) a provider can hide a concrete type ("pack"), and thus avoid any possibility of the client depending on it, and (b) a client can manipulate said type by only by giving it a name ("unpack") and making use of its bounds. Existentials play a big role in our understanding of abstract data types and encapsulation. - Burak Emir The take away:
  40. Existential types Attempt #3: Existentials http://www.drmaciver.com/2008/03/existential-types-in-scala/ scala> def foo(x: Array[T]

    forSome { type T}) = println(x.length) foo: (Array[T] forSome { type T })Unit scala> foo(Array("foo", "bar", "baz")) 3 Woohoo! Note that a commonly-used shorthand is: Array[_] Existential types provide a way of abstracting type information, such that (a) a provider can hide a concrete type ("pack"), and thus avoid any possibility of the client depending on it, and (b) a client can manipulate said type by only by giving it a name ("unpack") and making use of its bounds. Existentials play a big role in our understanding of abstract data types and encapsulation. - Burak Emir Code reuse: fully decouple implementation details from types The take away:
  41. Type classes Patterns: (ad-hoc polymorphism) Type classes enable retroactive extension.

    the ability to extend existing software modules with new functionality without needing to touch or re-compile the original source.
  42. Type classes? Interface: Implementation: trait Pickler[T] { def pickle(obj: T):

    Array[Byte] } implicit object intPickler extends Pickler[Int] { def pickle(obj: Int): Array[Byte] = { // logic for converting Int to Array[Byte] } } the “type class instance” the “type class”
  43. Implementation: implicit object intPickler extends Pickler[Int] { def pickle(obj: Int):

    Array[Byte] = { // logic for converting Int to Array[Byte] } } Type classes? Interface: trait Pickler[T] { def pickle(obj: T): Array[Byte] }
  44. Implementation: implicit object intPickler extends Pickler[Int] { def pickle(obj: Int):

    Array[Byte] = { // logic for converting Int to Array[Byte] } } Type classes? Interface: trait Pickler[T] { def pickle(obj: T): Array[Byte] } The first part is an interface containing one or more operations that should be provided by several different types. 1.
  45. Implementation: implicit object intPickler extends Pickler[Int] { def pickle(obj: Int):

    Array[Byte] = { // logic for converting Int to Array[Byte] } } Type classes? Interface: trait Pickler[T] { def pickle(obj: T): Array[Byte] } The first part is an interface containing one or more operations that should be provided by several different types. 1. Here, a pickle method should be provided for an arbitrary type, T.
  46. Interface: trait Pickler[T] { def pickle(obj: T): Array[Byte] } Implement

    that interface for different types. 2. Implementation: object intPickler extends Pickler[Int] { def pickle(obj: Int): Array[Byte] = { // logic for converting Int to Array[Byte] } } Type classes? Crucial: the correct implementation must be selected automatically based on type!
  47. Interface: trait Pickler[T] { def pickle(obj: T): Array[Byte] } Implement

    that interface for different types. 2. Implementation: implicit object intPickler extends Pickler[Int] { def pickle(obj: Int): Array[Byte] = { // logic for converting Int to Array[Byte] } } Type classes? Crucial: the correct implementation must be selected automatically based on type!
  48. Type classes? Interface: Implementation: trait Pickler[T] { def pickle(obj: T):

    Array[Byte] } implicit object intPickler extends Pickler[Int] { def pickle(obj: Int): Array[Byte] = { // logic for converting Int to Array[Byte] } }
  49. Using type classes? Example user code: def persist[T](obj: T)(implicit p:

    Pickler[T]): Unit = { val arr = obj.pickle // persist byte array `arr` } Type classes automate the selection of the implementation. Automatic selection is enabled by marking the pickler parameter as implicit!
  50. Using type classes? Example user code: def persist[T: Pickler](obj: T):

    Unit = { val arr = obj.pickle // persist byte array `arr` } Type classes automate the selection of the implementation. Shorthand with context bound!
  51. Using type classes? Example user code: def persist[T](obj: T)(implicit p:

    Pickler[T]): Unit = { val arr = p.pickle(obj) // persist byte array `arr` } Type classes automate the selection of the implementation. Now possible to invoke persist without passing a pickler implementation explicitly: persist(15) The type checker automatically infers the missing argument to be intPickler, purely based on its type.
  52. Example user code: def persist[T](obj: T)(implicit p: Pickler[T]): Unit =

    { val arr = p.pickle(obj) // persist byte array `arr` } Type classes automate the selection of the implementation. Now possible to invoke persist without passing a pickler implementation explicitly: persist(15) The type checker automatically infers the missing argument to be intPickler, purely based on its type. The take away: Type classes Patterns:
  53. Example user code: def persist[T](obj: T)(implicit p: Pickler[T]): Unit =

    { val arr = p.pickle(obj) // persist byte array `arr` } Type classes automate the selection of the implementation. Now possible to invoke persist without passing a pickler implementation explicitly: persist(15) The type checker automatically infers the missing argument to be intPickler, purely based on its type. Retroactively add functionality without having to recompile. The take away: Type classes Patterns:
  54. But there’s more. That’s about all I’ll cover. In addition

    there’s a bunch more one can do: Type-level programming. Type-based materialization with macros. Tricks with path-dependent types. You can always do lots of powerful stuff with type parameters/type members, bounds, variance, and type classes - all introduced here! That stuff is advanced. It’s not required knowledge to be a good Scala programmer. Higher-kinded types. If you’re interested, go forth, have fun!
  55. Resources for more advanced stuff That’s about all I’ll cover.

    Konrad Malawski has a wiki of type system constructs and patterns The Typelevel folks have an amazing blog! http://ktoso.github.io/scala-types-of-types/ http://typelevel.org/blog/