reducing an Array of Float using scala.math.max

前端 未结 4 1886
温柔的废话
温柔的废话 2021-01-06 09:53

I am confused by the following behavior - why does reducing an Array of Int work using math.max, but an Array of Float requires a wrapped function? I have memories that this

4条回答
  •  心在旅途
    2021-01-06 10:08

    It looks like this is a bug in the inferrer, cause with Int it infers types correctly:

    private[this] val res2: Int = scala.this.Predef.intArrayOps(scala.Array.apply(1, 2, 4)).reduce[Int]({
      ((x: Int, y: Int) => scala.math.`package`.max(x, y))
    });
    

    but with Floats:

    private[this] val res1: AnyVal = scala.this.Predef.floatArrayOps(scala.Array.apply(1.0, 3.0, 4.0)).reduce[AnyVal]({
      ((x: Int, y: Int) => scala.math.`package`.max(x, y))
    });
    

    If you explicitly annotate reduce with a Float type it should work:

    Array(1f, 3f, 4f).reduce[Float](max)
    
    private[this] val res3: Float = scala.this.Predef.floatArrayOps(scala.Array.apply(1.0, 3.0, 4.0)).reduce[Float]({
      ((x: Float, y: Float) => scala.math.`package`.max(x, y))
    });
    

提交回复
热议问题