Last active
August 29, 2015 14:10
-
-
Save adamw/46c6550a6444156b434e to your computer and use it in GitHub Desktop.
Classifiers in MacWire
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
/* | |
A proposition on how classifiers can be implemented in MacWire. Based on @milessabin's https://gist.github.com/milessabin/89c9b47a91017973a35f | |
*/ | |
// Infrastructure: would come in the library | |
// note that no changes in the macro are necessary. The qualifiers are purely type-based | |
type Tag[U] = { type Tag = U } | |
type @@[T, U] = T with Tag[U] | |
type Tagged[T, U] = T with Tag[U] | |
implicit class Tagger[T](t: T) { | |
def taggedWith[U]: T @@ U = t.asInstanceOf[T @@ U] | |
} | |
// Example usage: | |
class Berry() | |
trait Black | |
trait Blue | |
class Basket(blueberry: Berry @@ Blue, blackberry: Berry @@ Black) { | |
def consume(b: Berry) {} | |
consume(blueberry) // can be used as "just" berry | |
consume(blackberry) | |
} | |
// or | |
class Basket2(blueberry: Berry Tagged Blue, blackberry: Berry Tagged Black) | |
lazy val blueberry = wire[Berry].taggedWith[Blue] | |
lazy val blackberry = wire[Berry].taggedWith[Black] | |
lazy val basket = wire[Basket] | |
lazy val basket2 = wire[Basket2] | |
// Yes/no/would prefer annotations? | |
// One downside is that when declaring dependencies, you would need to use a type from Macwire (@@) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment