Created
February 12, 2014 21:26
-
-
Save suhailshergill/8964862 to your computer and use it in GitHub Desktop.
versionable things with access to version
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
object Def { | |
object WithoutEnum { | |
sealed trait Version { | |
val version: Version | |
} | |
trait Version1 extends Version { | |
val version = Version1 | |
} | |
implicit object Version1 extends Version1 | |
trait Version2 extends Version { | |
val version = Version2 | |
} | |
implicit object Version2 extends Version2 | |
trait Foo[T <: Version] { | |
def run()(implicit imp: T) = imp match { | |
case (x:Version1) => println("Version1") | |
case (x:Version2) => println("Version2") | |
} | |
} | |
object Foo1 extends Foo[Version1] | |
object Foo2 extends Foo[Version2] | |
} | |
object WithEnum { | |
sealed trait Version { | |
val version: Version.Version | |
} | |
private[Def] object Version extends Enumeration { | |
type Version = Value | |
val Version1, Version2 = Value | |
} | |
trait Version1 extends Version { | |
val version = Version.Version1 | |
} | |
implicit object Version1 extends Version1 | |
trait Version2 extends Version { | |
val version = Version.Version2 | |
} | |
implicit object Version2 extends Version2 | |
trait Foo[T <: Version] { | |
def run()(implicit imp: T) = println(imp.version) | |
} | |
object Foo1 extends Foo[Version1] | |
object Foo2 extends Foo[Version2] | |
} | |
} | |
object Use { | |
def run = { | |
Def.WithoutEnum.Foo1.run // prints Version1 | |
Def.WithoutEnum.Foo2.run // prints Version2 | |
Def.WithEnum.Foo1.run // prints Version1 | |
Def.WithEnum.Foo2.run // prints Version2 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment