Created
March 21, 2017 08:27
-
-
Save batakpout/a85e09e3ff29edbc963dcd592c08f4d0 to your computer and use it in GitHub Desktop.
Simple Generic Start In Scala
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
package generics | |
object TestProb1 extends App { | |
//type constraints | |
class D[T <: B] // T must be a B. i.e. for any T, it is a B or a subclass of B. | |
new D[B]() | |
new D[C]() | |
//new D[A]() CTE (type arguments [generics.A] do not conform to class D's type parameter bounds [T <: generics.B] | |
class E[T >: B] // T superclass of B or B must be a T. i.e. for any T, B must also be that same type. | |
new E[B]() | |
new E[A]() | |
//new E[C]() same error as above | |
} | |
class A | |
class B extends A | |
class C extends B | |
class TestProb2 extends App { | |
//covariance | |
class A[+T] | |
val az:A[Z] = new A[Z]() | |
//we can assign any A[T] to A[Y] as long as T <: Y. i.e if T is an Y, then any A[T] is an A[Y] | |
val ayz:A[Y] = az | |
// we can assign any A[T] to A[X] as long as T <: X. | |
val axy:A[X] = ayz | |
val axz:A[X] = az | |
//contravariance | |
class B[-T] | |
val ax:B[X] = new B[X]() | |
//we can assign any B[T] to B[Y] as long as T >: Y. i.e if Y is a T, then B[Y] is an B[T] | |
val ayx:B[Y] = ax | |
//we can assign any A[T] to A[Z} as long as T >: Z. i.e if Z is a T , then B[Z] is an B[T] | |
val azx:B[Z] = ax | |
val azy:B[Z] = ayx | |
val l = 1 :: Nil | |
} | |
trait X | |
trait Y extends X | |
trait Z extends Y |
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
package generics | |
object TestProb1 extends App { | |
//type constraints | |
class D[T <: B] // T must be a B. i.e. for any T, it is a B or a subclass of B. | |
new D[B]() | |
new D[C]() | |
//new D[A]() CTE (type arguments [generics.A] do not conform to class D's type parameter bounds [T <: generics.B] | |
class E[T >: B] // T superclass of B or B must be a T. i.e. for any T, B must also be that same type. | |
new E[B]() | |
new E[A]() | |
//new E[C]() same error as above | |
} | |
class A | |
class B extends A | |
class C extends B | |
class TestProb2 extends App { | |
//covariance | |
class A[+T] | |
val az:A[Z] = new A[Z]() | |
//we can assign any A[T] to A[Y] as long as T <: Y. i.e if T is an Y, then any A[T] is an A[Y] | |
val ayz:A[Y] = az | |
// we can assign any A[T] to A[X] as long as T <: X. | |
val axy:A[X] = ayz | |
val axz:A[X] = az | |
//contravariance | |
class B[-T] | |
val ax:B[X] = new B[X]() | |
//we can assign any B[T] to B[Y] as long as T >: Y. i.e if Y is a T, then B[Y] is an B[T] | |
val ayx:B[Y] = ax | |
//we can assign any A[T] to A[Z} as long as T >: Z. i.e if Z is a T , then B[Z] is an B[T] | |
val azx:B[Z] = ax | |
val azy:B[Z] = ayx | |
val l = 1 :: Nil | |
} | |
trait X | |
trait Y extends X | |
trait Z extends Y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment