title: Swift protocol ANY closure type
body: <p>So I was trying to make my own swift &quot;eval&quot; function, and my approach is to make some sort of class that accepts operators and then when you submit it a string of operations it can eval it into another type. My first step was to make an operator class so I could store operators. My approach was to make an operator protocol that required a string representation of the operator, ex: &quot;++&quot; (so that the operator can be recognized when evaluating a string), and a function called operate that actually executes the operation
  Here is the protocol I made:</p>
  <pre><code>protocol Operator {
      associatedtype returnType
      var id: String { get }
      var operate: (_ item: returnType) -&gt; returnType { get }
  }
  </code></pre>
  <p>Then, I went ahead and made a structure that conforms to Operator like so:</p>
  <pre><code>struct PrefixOperator&lt;T&gt;: Operator {
      var id: String
      var operate: (_ item: T) -&gt; T
  }
  </code></pre>
  <p>this worked as intended, however as I moved to make an infix operator which operates on two elements (for example: 5 - 3 were &quot;-&quot; is the operator)</p>
  <pre><code>struct InfixOperator&lt;T&gt;: Operator {
      var id: String
      var operate: (_ left: T, _right: T)
  }
  </code></pre>
  <p>I get an error message saying it does not conform to the protocol.
  I understand why it doesnt, and I think the solution is to make the protocol require any closure - let me just show you what I mean:</p>
  <pre><code>protocol Operator {
      associatedtype returnType
      var id: String { get }
      var operate: AnyClosure { get }
  }
  </code></pre>
  <p>However, looking through the docs I cannot find a type that represents a closure of any sort
  The answer Im looking for is if such a thing exists, and if it doesnt, I would really appreciate further guidance, maybe a tip from someone on how I could do this differently.</p>
  <p>Thank you for your time.</p>