title: Handle lots of similar monomorphic classes in a polymorphic way
body: <p>I've got a library which generates a whole bunch of very similar looking Java classes. For brevity, let's specify something like that in Kotlin:</p>
  
  <pre><code>data class A(val commonParam1: String, val commonParam2: String, val specialParam: Int)
  data class B(val commonParam1: String, val commonParam2: String, val specialParam: String)
  data class C(val commonParam1: String, val commonParam2: String, val specialParam: Boolean)
  data class D(val commonParam1: String, val commonParam2: String, val specialParam: Float)
  </code></pre>
  
  <p>Obviously, it would be preferable to generate a single generic class or interface:</p>
  
  <pre><code>data class G&lt;T&gt;(val commonParam1: String, val commonParam2: String, val specialParam: T)
  </code></pre>
  
  <p>But the library does not generate that kind of code. I have dozens of these classes, and different places at which they get generated, so it's an <em>m×n</em> situation. Unfortunately, these classes do not share a common interface (that I can use.) So the way I see it, there are five options to tackle this problem:</p>
  
  <ul>
  <li>write all the code that works with any of the generated classes <em>m×n</em> times. That's a whole bunch of duplication, but it is not at all sophisticated, and it is safe</li>
  <li>use reflection. The idea behind generating these classes in the first place was increased type safety. Reflection would throw that out of the window again, so it feels counter productive</li>
  <li>use code generation for my own code. It feels like a somewhat heavy-handed approach, and my team has no experience with code generation</li>
  <li>use <a href="https://arrow-kt.io/docs/typeclasses/intro/" rel="nofollow noreferrer">ArrowKT typeclasses</a>. <a href="https://github.com/apollographql/apollo-android" rel="nofollow noreferrer">Extension Interfaces</a> would be great, but I might get by with the currently existing hacks. I still have to provide a lot of boiler plate, because I have to provide evidence for my interface for each and every one of these classes</li>
  <li>use an <a href="https://github.com/arrow-kt/arrow-meta" rel="nofollow noreferrer">Arrow-Meta</a> quasi-quoter, but I don't think I should be using Arrow-Meta in production yet</li>
  </ul>
  
  <p>Do I have other options? Is there a way to abstract over similar looking classes like that?</p>
  
  <hr>
  
  <p>In this concrete case, my Graphql schema has a lot of mutations, which take <code>MutationInputs</code> that are all very similar. We don't directly control the shape of the schema. Since each mutation also has its own input and output type, constructing these mutations and interpreting their outputs is a chore with a lot of duplicated code even when I use fragments liberally, since even the code to extract the fragment will have to be duplicated.</p>
  
  <p>This is not usually a problem in Graphql because most of it is done in JS or TS, both of which have duck typing. Kotlin doesn't have duck typing.</p>