title: Is it possible to define [Int] * Int using custom operator in Swift?
body: <p>I want to define a new operator and multiply each element of the array <code>[Int]</code> by <code>Int</code>, such as <code>[3, 2, 10] * 10</code>.</p>
  
  <p>However, because <code>Int</code> is neither protocol nor class (it's struct), I first defined the following:</p>
  
  <pre><code>protocol Numeric {}
  extension Int: Numeric {}
  extension Double: Numeric {}
  extension Float: Numeric {}
  </code></pre>
  
  <p>And then, I tried defining the operator, like:</p>
  
  <pre><code>func *&lt;T: Numeric&gt; (left: [T], right: T) -&gt; [T] {
      var newArray: [T] = []
      for i in left {
          newArray.append(i * right)
      }
  
      return newArray
  }
  </code></pre>
  
  <p>However, this spits out an error: <code>Cannot convert value of type 'T' to expected argument type '[_]'</code>.</p>
  
  <p>I'm not sure what the type <code>[_]</code> means, which I don't <em>expect</em>, but I guess the problem comes from that I don't have an operator defined that takes <code>T</code> and <code>T</code>, both of which are <code>Numeric</code> in this case.</p>
  
  <p>So I defined another operator, like:</p>
  
  <pre><code>func *&lt;T: Numeric&gt; (left: T, right: T) -&gt; T {
      return left * right
  }
  </code></pre>
  
  <p>However, while this has been compiled without problems, the runtime error occurred with a lot of a lot of <code>static * infix &lt;A where ...&gt; (A, A) -&gt; A</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/7sNzk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7sNzk.png" alt="enter image description here"></a></p>
  
  <p>I'm not sure why this operator was executed so many times, but now I wonder if it is possible in the first place to define a custom <code>*</code> operator, although the <code>Int</code> does already have <code>*</code> operator defined.</p>
  
  <p>So is it still possible to define <code>[Int] * Int</code> operator in Swift?</p>