title: Making Swift generics play with overloaded functions
body: <p>I'm trying to build a generic type <code>MyStruct&lt;T&gt;</code> that can use either <code>Float</code> or <code>Double</code> for internal storage.  In the initializer, I'm passing an argument of type T (which I intend to be either <code>Float</code> or <code>Double</code>).  That initializer calls some trig functions such as <code>sin()</code> and <code>cos()</code>.  Both of those functions are overloaded in the system libraries to provide <code>Float</code> and <code>Double</code> versions.</p>
  
  <pre><code>var f:Float=1.2
  var d:Double=1.2
  
  sin(f) //0.9320391
  sin(d) //0.9320390859672263
  </code></pre>
  
  <p>Trouble is, I can't use them in my generic struct.  A stripped down case would look like this:</p>
  
  <pre><code>struct MyStruct&lt;T&gt; {
      var v:T
  
      init(x:T){v=sin(x)}
  }
  </code></pre>
  
  <p>Because: </p>
  
  <blockquote>
    <p>Playground execution failed: Untitled Page.xcplaygroundpage:9:17:
    error: cannot invoke 'sin' with an argument list of type '(T)'
        init(x:T){v=sin(x)}</p>
    
    <p>Untitled Page.xcplaygroundpage:9:17: note: overloads for 'sin' exist with these partially matching parameter lists:
    (Float), (Double)
        init(x:T){v=sin(x)}</p>
  </blockquote>
  
  <p>It seems like there should be a way to make this work, but it feels a lot like <a href="https://stackoverflow.com/q/37339438/2571165">this situation</a>.  Where the comments suggest there is no way to require the existence of a global function.</p>
  
  <p>I can force the situation by using a construct such as:</p>
  
  <pre><code>init(x:T){v=T(sin(Double(x)))}
  </code></pre>
  
  <p>and putting a constraint on T that it can be constructed from a <code>Double</code>, but that seems to defeat the purpose of making the <code>Float</code> version of the struct, which is to reduce computation effort when this is used in a critical loop of code.</p>
  
  <p>It feels like this would have been easier if <code>sin()</code> had been defined in the library as a generic function rather than an overloaded function:</p>
  
  <pre><code>func sin&lt;T:FloatingPointType&gt; (x:T) -&gt; T
  </code></pre>
  
  <p>but it is what it is.  </p>
  
  <p>Is there a way for me to build a library that is Float/Double agnostic on top of the standard libraries without adding a lot of overhead?</p>