Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. interface Set<out E> : Collection<E>
  2.  
  3. abstract fun contains(element: E): Boolean
  4. abstract fun containsAll(elements: Collection<E>): Boolean
  5.  
  6. class StringSet : Set<String> {
  7. override val size = 2
  8. override fun contains(element: String): Boolean {
  9. println("--- StringSet.contains($element)")
  10. return element == "Hallo" || element == "World"
  11. }
  12.  
  13. override fun containsAll(elements: Collection<String>) : Boolean =
  14. elements.all({it -> contains(it)})
  15. override fun isEmpty() = false
  16. override fun iterator() = listOf("Hallo", "World").iterator()
  17.  
  18. }
  19.  
  20. fun main() {
  21. val sset : Set<String> = StringSet()
  22. println(sset.contains("Hallo"))
  23. println(sset.contains("xxx"))
  24. //// compiler error:
  25. // println(set.contains(5))
  26.  
  27. val aset : Set<Any> = sset
  28. println(aset.contains("Hallo"))
  29. println(aset.contains("xxx"))
  30. // this compiles (and returns false), but the method is not actually called
  31. println(aset.contains(5))
  32. }
  33.  
  34. abstract fun contains(element: @UnsafeVariance E): Boolean
  35. abstract fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement