title: Kotlin: Making subclasses of generic types inherit functions and restricting the available types of output
body: <p>I have the following Kotlin classes implementing something similar to the Collections interface or a set that contains multiple elements.</p>
  
  <pre><code>abstract class MyCollection&lt;C: MyCollection&lt;C&gt;&gt; {
      abstract fun contains(e: Member&lt;C&gt;): Member&lt;CollectionOfBooleanValues&gt;
      //Function that determines if a Member&lt;C&gt; is present in this collection.
      // Maps to a collection of boolean values (True, False and Unsure)
  
      abstract fun &lt;E: MyCollection&lt;E&gt;&gt; mapToOtherCollection(f: (Member&lt;C&gt;) -&gt; Member&lt;E&gt;): E
      //Function that return a collection of the element that are created from
      //a mapping of this collection through a function f.
      //This could for example be the owners of the things of this collection, provided we
      //have a Persons class extends MyCollection. Or a collection of all colors of the things in this collection.
  }
  
  abstract class BigCollection&lt;C: BigCollection&lt;C&gt;&gt;: MyCollection&lt;C&gt;() {
      override fun contains(e: Member&lt;C&gt;): Member&lt;CollectionOfBooleanValues&gt; = Unsure
  
      abstract override fun &lt;E: MyCollection&lt;E&gt;&gt; mapToOtherCollection(f: (Member&lt;C&gt;) -&gt; Member&lt;E&gt;): E
  }
  
  abstract class SmallCollection&lt;C: SmallCollection&lt;C&gt;&gt;(val choices: List&lt;Member&lt;C&gt;&gt;): BigCollection&lt;C&gt;() {
      override fun contains(e: Member&lt;C&gt;): Member&lt;CollectionOfBooleanValues&gt; =
          if(choices.contains(e))
              True
          else
              False
  }
  
  abstract class Persons&lt;D: Persons&lt;D&gt;&gt;: MyCollection&lt;D&gt;() {
      override fun contains(e: Member&lt;D&gt;): Member&lt;CollectionOfBooleanValues&gt; {
          return True
      }
  }
  
  abstract class Member&lt;D: MyCollection&lt;D&gt;&gt;(sym: String, val collectionType: D) {
  }
  
  object CollectionOfBooleanValues: SmallCollection&lt;CollectionOfBooleanValues&gt;(choices = listOf(True, False, Unsure)){
      override fun &lt;E: MyCollection&lt;E&gt;&gt; mapToOtherCollection(f: (Member&lt;CollectionOfBooleanValues&gt;) -&gt; Member&lt;E&gt;): E =
              throw RuntimeException("Not implemented");
  }
  
  object True: Member&lt;CollectionOfBooleanValues&gt;("true", CollectionOfBooleanValues)
  object False: Member&lt;CollectionOfBooleanValues&gt;("false", CollectionOfBooleanValues)
  object Unsure: Member&lt;CollectionOfBooleanValues&gt;("unsure", CollectionOfBooleanValues)
  </code></pre>
  
  <p>So instances of one of MyCollection's subclasses describe some sort of set and instances of the class Member&lt; C > describe a particular member of that collection.
  The subclasses BigCollection and SmallCollection determine what size of collection we have. My idea is to add more here depending on how hard the collections are to work with.</p>
  
  <p>One could imagine the following examples</p>
  
  <p>Red could be an instance/object of the type Member&lt; Color ></p>
  
  <p>Persons could be a subtype of the type SmallCollection&lt; Persons ></p>
  
  <p>Color could be a subtype of the type BigCollection&lt; Color > and be essentially infinite. This means for example that the function contains(Member&lt; Color >) can never return false, only Unsure or possibly True if it found it and throw an exception after a specific timeout.</p>
  
  <p>Just so you have a basic idea of what I'm trying to do.</p>
  
  <p>Notice how the JVM's type erasure forces me to use recursive generics if I want to have access to the type MyCollection&lt; C > at both compile time and runtime.</p>
  
  <p>We can see that it's fairly type safe. We know that mapToOtherDomain will return an E, which is a subtype of MyCollection&lt; D > and it's going to be the same type as the E in the function (Member&lt; C >) -> Member&lt; E >) we put in.</p>
  
  <p>Now, what would be neat would be if the subclasses of MyCollection could override the function mapToOtherCollection, so that the size is reflected in the static signature of the function. The function f is a one-to-one mapping so if we map a smallCollection of type A to B I want the output to be a smallCollection of B.</p>
  
  <p>I feel like this should be possible, especially since it's almost possible in Java, and Kotlin is supposed to be an extension on Java's generic type system. I can't get it to work though. I want it to override, but still restrict the return type (and thus also the input type)</p>
  
  <p>I've played around with method signatures like this:</p>
  
  <pre><code>abstract class BigCollection&lt;C: BigCollection&lt;C&gt;&gt;: MyCollection&lt;C&gt;() {
      abstract override fun &lt;E: BigCollection&lt;E&gt;&gt; mapToOtherCollection(f: (Member&lt;C&gt;) -&gt; Member&lt;E&gt;): E
  }
  </code></pre>
  
  <p>which gives the compiler error that it doesn't override the previous method, and like this</p>
  
  <pre><code>abstract class MyCollection&lt;C: MyCollection&lt;C&gt;&gt; {
      abstract fun &lt;E: MyCollection&lt;E&gt;&gt; mapToOtherCollection(f: (Member&lt;C&gt;) -&gt; Member&lt;E&gt;): MyCollection&lt;E&gt;
  }
  
  abstract class BigCollection&lt;C: BigCollection&lt;C&gt;&gt;: MyCollection&lt;C&gt;() {
      abstract override fun &lt;E: MyCollection&lt;E&gt;&gt; mapToOtherCollection(f: (Member&lt;C&gt;) -&gt; Member&lt;E&gt;): BigCollection&lt;E&gt;
  }
  </code></pre>
  
  <p>which says The Type argument is out of bounds.</p>
  
  <p>How can I just restrict the in/out types to better match the subclass of the generic class in this example? It would also be neat if the compiler knew that the output of contains() in a BigCollection is going to be Unsure statically rather that dynamically.</p>
  
  <p>Thanks</p>