Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.