Skip to content

Instantly share code, notes, and snippets.

@xeno-by
Created April 10, 2014 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeno-by/10361173 to your computer and use it in GitHub Desktop.
Save xeno-by/10361173 to your computer and use it in GitHub Desktop.
// *** What we want to do ***
// Write a mini-cake that would take and return path-dependent universe artifacts
// retaining precise path-dependent types when returning values
trait Universe {
type Tree
}
object ru extends Universe {
class Tree
}
// Attempt #1: Use refinements
object Helper1 {
def apply(u0: Universe): Helper1 { val u: u0.type } = new { val u: u0.type = u0 } with Helper1
}
trait Helper1 {
val u: Universe
def foo: u.Tree = ???
}
object Test1 {
val foo1 = Helper1(ru).foo // type of foo1: _1.u.Tree forSome { val _1: Helper1{val u: ru.type} }
}
// Approach #2: Use a singleton type parameter
object Helper2 {
def apply[U <: Universe with Singleton](u0: U): Helper2[U] = new { val u = u0 } with Helper2[U]
}
trait Helper2[U <: Universe with Singleton] {
val u: U
def foo: u.Tree = ???
}
object Test2 {
val foo2 = Helper2(ru).foo // type of foo2: _2.u.Tree forSome { val _2: Helper2[ru.type] }
}
// Approach #3: Use a singleton type parameter and a type projection
object Helper3 {
def apply[U <: Universe with Singleton](u0: U): Helper3[U] = new { val u = u0 } with Helper3[U]
}
trait Helper3[U <: Universe with Singleton] {
val u: U
def foo: U#Tree = ???
}
object Test3 {
val foo3 = Helper3(ru).foo // type of foo3: ru.Tree
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment