title: How to restrict the generic extension function parameter type?
body: <p>I try to use the list extension function binarySearch</p>
  
  <pre><code>public fun &lt;T&gt; List&lt;T&gt;.binarySearch(element: T, comparator: Comparator&lt;in T&gt;,
  fromIndex: Int = 0, toIndex: Int = size): Int
  </code></pre>
  
  <p>This is my definition:</p>
  
  <pre><code>open class Z
  open class ZZ : Z()
  open class ZZZ : ZZ()
  open class ZZZZ : ZZZ()
  
  val list: List&lt;ZZZZ&gt; = listOf()
  val a0 = list.binarySearch("text", stringComparator) &lt;-- error
  val a1 = list.binarySearch("text", anyComparator)
  val a2 = list.binarySearch(Z(), zComparator)
  val a3 = list.binarySearch(ZZ(), zzComparator)
  val a4 = list.binarySearch(ZZZ(), zzzComparator)
  
  val stringComparator = Comparator&lt;String&gt; { o1, o2 -&gt; o1.length.compareTo(o2.length) }
  val anyComparator = Comparator&lt;Any&gt; { o1, o2 -&gt; o1.hashCode().compareTo(o2.hashCode()) }
  val zComparator = Comparator&lt;Z&gt; { o1, o2 -&gt; o1.hashCode().compareTo(o2.hashCode()) }
  val zzComparator = Comparator&lt;ZZ&gt; { o1, o2 -&gt; o1.hashCode().compareTo(o2.hashCode()) }
  val zzzComparator = Comparator&lt;ZZZ&gt; { o1, o2 -&gt; o1.hashCode().compareTo(o2.hashCode()) }
  </code></pre>
  
  <p>The function binarySearch can not accept the String type but can accept the supertype of ZZZZ. I think the first parameter only accept ZZZZ type because List &lt; T > is defined, but why it can accept all the supertype of ZZZZ?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I create a list extension function like this:</p>
  
  <pre><code>fun &lt;T&gt; List&lt;T&gt;.ccc(t: T): Boolean {
      return contains(t)
  } 
  
  val list = listOf&lt;ZZZZ&gt;()
  list.ccc("Q")
  list.ccc(0)
  list.ccc(mutableListOf&lt;String&gt;())
  list.ccc(Z())
  list.ccc(ZZ())
  list.ccc(ZZZ())
  </code></pre>
  
  <p>I found it can accept many kind of type, the related question like this:
  <a href="https://stackoverflow.com/questions/57224118/how-to-write-strongly-typed-generic-extension-function-in-kotlin">How to write strongly typed generic extension function in Kotlin?</a></p>
  
  <p>Is the <code>binarySearch()</code> function same as the <code>ccc()</code>?</p>
  
  <p>Why <code>ccc()</code> can accept not only ZZZZ's supertype but also others?</p>
  
  <p>Thanks for your help again!</p>