Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Text
Posted by: Pahay Ragh
Added: Oct 26, 2020 4:35 AM
Views: 13
Tags: scala
  1. class Super
  2. class Sub extends Super
  3. class SubSub extends Sub
  4.  
  5. class InvariantSubType[A]{
  6.   def method[B <: A](value :B) = value
  7. }
  8.  
  9. val invariantSubType1 = new InvariantSubType[Sub]
  10. val res1 = invariantSubType1.method(new SubSub)
  11.  
  12. val invariantSubType2 = new InvariantSubType[SubSub]
  13. //val res2 = invariantSubType2.method(new Sub)  // not a subtype
  14.  
  15. class InvariantSuperType[A]{
  16.   def method[B >: A](value :B) = value
  17. }
  18.  
  19. //implicit def a2b(a:Sub) = new SubSub
  20.  
  21. val invariantSuperType1 = new InvariantSuperType[Sub]
  22. //val res12:SubSub = invariantSuperType1.method(new SubSub)  // SubSub is not Super Type
  23.  
  24. val invariantSuperType2 = new InvariantSuperType[SubSub]
  25. val res13 = invariantSuperType2.method(new Sub)
  26.  
  27. class UpperBound[+A]{
  28.   def method[B >: A](value :B) = value
  29. }
  30.  
  31. val test2242 = new UpperBound[Sub]
  32. //val res:SubSub = test2242.method(new SubSub) // SubSub is not Super Type
  33.  
  34. val test2122:UpperBound[SubSub] = new UpperBound[SubSub]
  35. val res1:Sub = test2122.method(new Sub)
  36.  
  37.  
  38. class UpperBound[-A]{
  39.   def method[B <: A](value :B) = value
  40. }
  41.  
  42. val test224 = new UpperBound[Sub]
  43. val res:SubSub = test224.method(new SubSub)
  44. val test2123:UpperBound[SubSub] = new UpperBound[SubSub]
  45. //val res1:Sub = test2123.method(new Sub)  // Not a Subtype