Created
October 15, 2017 14:58
-
-
Save abdheshkumar/2bf234e35b2f4a40df187772e2f9d1ed to your computer and use it in GitHub Desktop.
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
trait Item | |
trait PlasticItem extends Item | |
trait PlasticBottle extends PlasticItem | |
trait PaperItem extends Item | |
trait NewsPaper extends PaperItem | |
class GarbageCan[-A] {} | |
// PlasticItem <: Item, GarbageCan[Item] <: GarbageCan[PlasticItem] it has allowed because Function1[-T,+R] | |
val filter = new Function1[GarbageCan[PlasticItem], Boolean] { | |
override def apply(v1: GarbageCan[PlasticItem]): Boolean = true | |
} | |
filter(new GarbageCan[Item]) | |
filter(new GarbageCan[PlasticItem]) | |
//filter(new GarbageCan[PlasticBottle]) // I know Why It is failing | |
List(new GarbageCan[PlasticItem], new GarbageCan[Item]).filter(filter) //works | |
// PlasticItem <: Item, GarbageCan[B] <: GarbageCan[A] | |
val filter1 = new Function1[PlasticItem, Boolean] { | |
override def apply(v1: PlasticItem): Boolean = ??? | |
} | |
//List(new PlasticItem {}, new Item {}).filter(filter1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment