Skip to content

Instantly share code, notes, and snippets.

@abdheshkumar
Created October 15, 2017 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdheshkumar/2bf234e35b2f4a40df187772e2f9d1ed to your computer and use it in GitHub Desktop.
Save abdheshkumar/2bf234e35b2f4a40df187772e2f9d1ed to your computer and use it in GitHub Desktop.
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