Created
April 10, 2014 09:27
-
-
Save xeno-by/10361240 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// *** 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
Please reply at https://groups.google.com/forum/#!topic/scala-internals/kMpBWLkPz38. I'm not getting notifications about comments to gists, so I will most likely overlook them.