Created
April 20, 2016 16:41
-
-
Save ccw/4e43028b06b1969b01078c86089e3d45 to your computer and use it in GitHub Desktop.
type parameters and type fields
This file contains 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
trait Service { | |
type A | |
type B | |
def getA:Option[A] | |
def getB:Option[B] | |
} | |
trait SService[AA, BB] extends Service { | |
type A = AA | |
type B = BB | |
} | |
abstract class AbstractService[AA, BB <: String](x:String) extends SService[AA, BB] { | |
override def getA: Option[A] = None | |
override def getB: Option[B] = None | |
} | |
trait ServiceProxy[M <: Service { | |
type A | |
type B <: String | |
}] { | |
type proxy = AbstractService[M#A, M#B] | |
} | |
trait ServiceWrapper[M[_, _ <: String]] { | |
type wrapped[A, B <: String] = M[A, B] | |
} | |
object test { | |
type StringService = SService[String, String] | |
} | |
object Main extends App { | |
type ToS = ServiceProxy[StringService]#proxy | |
type ToW = ServiceWrapper[AbstractService]#wrapped[String, String] | |
class S extends ToS("test") { | |
} | |
class T[A, B <: String] extends AbstractService[A, B]("test") | |
val l = List[StringService](new S) | |
println(l) | |
println(l.head.isInstanceOf[AbstractService[String, String]]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment