title: Swift Equatable Generic type comparison inside generic function
body: <p>I have a <code>Node</code> class for a binary tree like so:</p>
  
  <pre><code>class Node&lt;T: Equatable&gt; {
      let value: T
      let left: Node&lt;T&gt;?
      let right: Node&lt;T&gt;?
  
      init(value: T, left: Node&lt;T&gt;? = nil, right: Node&lt;T&gt;? = nil) {
          self.value = value
          self.left = left
          self.right = right
      }
  }
  </code></pre>
  
  <p>The values need to be equatable.</p>
  
  <p>I can test out the equitability like this:</p>
  
  <pre><code>let a = Node(value: 8)
  let b = Node(value: 7)
  
  let c = a.value &gt; b.value
  </code></pre>
  
  <p>Which works fine, <code>c: true</code></p>
  
  <p>But when I write a generic function that uses the equitability of the nodes I get errors:</p>
  
  <pre><code>func isBinaryTree&lt;T&gt;(node: Node&lt;T&gt;) -&gt; Bool {
      if let leftNode = node.left {
          guard leftNode.value &lt; node.value else {
              return false
          }
          guard isBinaryTree(node: leftNode) else {
              return false
          }
      }
      if let rightNode = node.right {
          guard rightNode.value &gt;= node.value else {
              return false
          }
          guard isBinaryTree(node: rightNode) else {
              return false
          }
      }
  
      return true
  }
  
  let result = isBinaryTree(node: root)
  </code></pre>
  
  <p>Errors:</p>
  
  <pre><code>error: binary operator '&lt;' cannot be applied to two 'T' operands guard leftNode.value &lt; node.value ||`
  </code></pre>
  
  <p>I'm not sure why the compiler doesn't seem to know why the <code>T</code> values are <code>Equatable</code> or why it doesn't think that the <code>T</code> on the <code>leftNode</code> is the same type as the <code>T</code> on the <code>node</code>.</p>
  
  <p>The code: </p>
  
  <pre><code>let d = Node(value: Float(3), left: Node(value: Int(8)) , right: nil)
  </code></pre>
  
  <p>Gives an error as expected.</p>
  
  <p>Looking further into this, it isn't related to the function because when I try the code:</p>
  
  <pre><code>let x = Node(value: 3, left: Node(value: 8) , right: nil)
  let y = x.value &lt; x.left!.value
  </code></pre>
  
  <p>I get the same error</p>