Dotty and types:
the story so far

                                    Guillaume Martres - EPFL

Takeaway message

                     Hacking compilers is fun and profitable!

Typing example


def foo[T](x: T): T = x

foo(1) // How is this typed?
              
1. Typing foo creates a type variable T?

foo[T?]
              
2. Typing foo[T?](1) adds a constraint to T?

foo[T?](1) // T? >: Int
                
3. Instantiating T? = Int respects the constraints

foo[Int](1)
                

Safe instantiation


def foo[T](…): RetType = …
foo[T?](…) // T? >: Lo <: Hi
  .bar(…)
                
  • Can we instantiate T? right after typing foo[T?](…) ?
  • It depends on where T appears in RetType
T appears Safe? T? =
nowhere Yes Lo (arbitrarily)
covariantly Yes Lo
contravariantly Yes Hi
co- and contravariantly
or invariantly
No ¯\_(ツ)_/¯