Skip to content

Instantly share code, notes, and snippets.

@rupeshtr78
Last active October 29, 2020 00:57
Show Gist options
  • Save rupeshtr78/adfeef048d45563a0738137cb3453164 to your computer and use it in GitHub Desktop.
Save rupeshtr78/adfeef048d45563a0738137cb3453164 to your computer and use it in GitHub Desktop.
VarianceWithBounds
class Super
class Sub extends Super
class SubSub extends Sub
class InvariantSubType[A]{
def method[B <: A](value :B) = value
}
val invariantSubType1 = new InvariantSubType[Sub]
val res1 = invariantSubType1.method(new SubSub)
val invariantSubType2 = new InvariantSubType[SubSub]
//val res2 = invariantSubType2.method(new Sub) // not a subtype
class InvariantSuperType[A]{
def method[B >: A](value :B) = value
}
//implicit def a2b(a:Sub) = new SubSub
val invariantSuperType1 = new InvariantSuperType[Sub]
//val res12:SubSub = invariantSuperType1.method(new SubSub) // SubSub is not Super Type
val invariantSuperType2 = new InvariantSuperType[SubSub]
val res13 = invariantSuperType2.method(new Sub)
class UpperBound[+A]{
def method[B >: A](value :B) = value
}
val test2242 = new UpperBound[Sub]
//val res:SubSub = test2242.method(new SubSub) // SubSub is not Super Type
val test2122:UpperBound[SubSub] = new UpperBound[SubSub]
val res1:Sub = test2122.method(new Sub)
class UpperBound[-A]{
def method[B <: A](value :B) = value
}
val test224 = new UpperBound[Sub]
val res:SubSub = test224.method(new SubSub)
val test2123:UpperBound[SubSub] = new UpperBound[SubSub]
//val res1:Sub = test2123.method(new Sub) // Not a Subtype
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment