title: How to constrain types to specific types in scala
body: <p>I'd like to adapt this code as given here : <a href="https://stackoverflow.com/questions/20285209/find-min-and-max-elements-of-array/20285586#20285586">Find min and max elements of array</a></p>
  <pre><code>def minMax(a: Array[Int]) : (Int, Int) = {
    if (a.isEmpty) throw new java.lang.UnsupportedOperationException(&quot;array is empty&quot;)
    a.foldLeft((a(0), a(0)))
    { case ((min, max), e) =&gt; (math.min(min, e), math.max(max, e))}
  }
  </code></pre>
  <p>To work also with <code>Long</code>, <code>Float</code> and <code>Double</code> (since these are the types accepted by scala.math.min/max. I tried:</p>
  <pre><code>def getMinAndMax[@specialized(Int,Long,Float,Double) T](x: Seq[T]) : (T, T) = {
    if (x.isEmpty) throw new java.lang.UnsupportedOperationException(&quot;seq is empty&quot;)
    x.foldLeft((x.head, x.head))
    { case ((min, max), e) =&gt; (math.min(min, e), math.max(max, e))}
  }
  </code></pre>
  <p>But this does not compile either. Any advice?</p>