Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Set<out E> : Collection<E>
- abstract fun contains(element: E): Boolean
- abstract fun containsAll(elements: Collection<E>): Boolean
- class StringSet : Set<String> {
- override val size = 2
- override fun contains(element: String): Boolean {
- println("--- StringSet.contains($element)")
- return element == "Hallo" || element == "World"
- }
- override fun containsAll(elements: Collection<String>) : Boolean =
- elements.all({it -> contains(it)})
- override fun isEmpty() = false
- override fun iterator() = listOf("Hallo", "World").iterator()
- }
- fun main() {
- val sset : Set<String> = StringSet()
- println(sset.contains("Hallo"))
- println(sset.contains("xxx"))
- //// compiler error:
- // println(set.contains(5))
- val aset : Set<Any> = sset
- println(aset.contains("Hallo"))
- println(aset.contains("xxx"))
- // this compiles (and returns false), but the method is not actually called
- println(aset.contains(5))
- }
- abstract fun contains(element: @UnsafeVariance E): Boolean
- abstract fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement