Last active
March 10, 2018 02:26
-
-
Save milessabin/6256495 to your computer and use it in GitHub Desktop.
Path dependent types with an implicit prefix. We can infer the singleton type prefix of a path dependent type and then implicitly summon the unique value of that singleton type.
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
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_21). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> class Prefix { class Dep { type P = Prefix.this.type } } | |
defined class Prefix | |
scala> val (p1, p2) = (new Prefix, new Prefix) | |
p1: Prefix = Prefix@2212c414 | |
p2: Prefix = Prefix@7e070e85 | |
scala> val (d1, d2) = (new p1.Dep, new p2.Dep) | |
d1: p1.Dep = Prefix$Dep@798ab9b2 | |
d2: p2.Dep = Prefix$Dep@2a59375 | |
scala> def prefix[P <: Prefix](dep: P#Dep)(implicit p: dep.P): P = p // Infer singleton type P, then summon it. | |
prefix: [P <: Prefix](dep: P#Dep)(implicit p: dep.P)P | |
scala> implicit val ip1 = p1 | |
ip1: Prefix = Prefix@2212c414 | |
scala> implicit val ip1: p1.type = p1 | |
ip1: p1.type = Prefix@2212c414 | |
scala> implicit val ip2: p2.type = p2 | |
ip2: p2.type = Prefix@7e070e85 | |
scala> prefix(d1) | |
res0: Prefix = Prefix@2212c414 | |
scala> prefix(d2) | |
res1: Prefix = Prefix@7e070e85 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment