title: Family Polymorphism in Scala
body: <p>What is the current recommended pattern for family polymorphism in Scala?</p>
  
  <p>While experimenting with ways of modeling games, this solution recently emerged:</p>
  
  <pre><code>trait Game[G &lt;: Game[G]] {
  
    type PLAYER &lt;: Player[G]
    type STATE &lt;: State[G]
  
    def players(): Set[G#PLAYER]
  
    def startState(): G#STATE
  }
  
  trait Player[G &lt;: Game[G]]
  
  trait State[G &lt;: Game[G]] {
    def player(): G#PLAYER
  }
  </code></pre>
  
  <p>A specific game (Poker in this example) can be expressed in terms of those traits like so:</p>
  
  <pre><code>class Poker() extends Game[Poker] {
  
    type PLAYER = PokerPlayer
    type STATE = PokerState
  
    val p1 = new PokerPlayer()
  
    def players() = Set(p1)
  
    def startState(): PokerState = ...
  }
  
  class PokerPlayer() extends Player[Poker]
  
  class PokerState() extends State[Poker] {
    def player(): PokerPlayer = ...
  }
  </code></pre>
  
  <p>I have several questions about this setup:</p>
  
  <ol>
  <li><p>How is <code>Game[G &lt;: Game[G]]</code> pronounced <em>in English</em>?  What are the names for the roles that <code>G</code> and <code>Game</code> are playing in this situation? (Meaning specifically in this "recursive" relationship.)</p></li>
  <li><p>Is this a reasonable implementation of "family polymorphism"?  At a high level, my understanding is that this means that Game and it's PLAYER and STATE must vary as a "family".  The takes on family polymorphism in Scala that I've seen elsewhere differ substantially, and I'm not clear on the different tradeoffs:</p>
  
  <ul>
  <li><p>Scala Overview (2006) <a href="http://www.scala-lang.org/docu/files/ScalaOverview.pdf">http://www.scala-lang.org/docu/files/ScalaOverview.pdf</a></p></li>
  <li><p>Martin Kneissl blog (2009) <a href="http://www.familie-kneissl.org/Members/martin/blog/family-polymorphism-in-scala">http://www.familie-kneissl.org/Members/martin/blog/family-polymorphism-in-scala</a></p></li>
  </ul></li>
  <li><p>Discussion of approaches to family polymorphism involving typeclasses, macros, f-bounded polymorphism, or anything else are welcome.</p></li>
  </ol>