title: Swift: Can't use nested/chained generic type constraints (Policy pattern)
body: <p>I'm implementing a randomized tree data structure in Swift. To constrain tree's width and depth, I have decided to use the <em>Policy (Strategy) pattern</em>.</p>
  
  <p>My policy looks like this:</p>
  
  <pre><code>protocol BaseTreeNodePolicy {
      static var maximumDepth: Int { get }
      static var maximumWidth: Int { get }
  }
  </code></pre>
  
  <p>and I have a base class which looks like this:</p>
  
  <pre><code>class BaseTreeNode&lt;PolicyType: BaseTreeNodePolicy&gt; { /* ... */ }
  </code></pre>
  
  <p>some subclasses of it:</p>
  
  <pre><code>class ValueNode&lt;ValueType, PolicyType: BaseTreeNodePolicy&gt;: BaseTreeNode&lt;PolicyType&gt; { /* ... */ }
  class ActionNode&lt;PolicyType: BaseTreeNodePolicy&gt;: BaseTreeNode&lt;PolicyType&gt; { /* ... */ }
  </code></pre>
  
  <p>Everything has worked well until I added this class:</p>
  
  <pre><code>final class SequenceNode&lt;ChildType: ActionNode&lt;PolicyType&gt;, PolicyType: BaseTreeNodePolicy&gt;: ActionNode&lt;PolicyType&gt; {
      public var sequence: [ChildType] = []
      // ...
      public override func addRandomDescendants(generator: EntropyGenerator, maxDepth: Int) {
          // ...
          sequence.reserveCapacity(42)
          // ...
          let descendant = ChildType(generator: generator, maxDepth: maxDepth - 1)
          // ...
      }
  }
  </code></pre>
  
  <p>When compiling <code>SequenceNode</code>, I get compiler errors on this row:</p>
  
  <pre><code>// 'ChildType' is not a subtype of 'ActionNode&lt;PolicyType&gt;'
  sequence.reserveCapacity(42)
  </code></pre>
  
  <p>and this row:</p>
  
  <pre><code>// Type 'PolicyType' does not conform to protocol 'BaseTreeNodePolicy'
  let descendant = ChildType(generator: generator, maxDepth: maxDepth - 1)
  </code></pre>
  
  <p>I have no idea what's wrong as I clearly state the type requirements saying that <code>ChildType: ActionNode&lt;PolicyType&gt;</code> and <code>PolicyType: BaseTreeNodePolicy</code> right in the header part of the declaration.</p>
  
  <p>What could be wrong?</p>
  
  <p>Thanks in advance!
  Petr.</p>