开发者

Scala: why Float.floatToIntBits(2f) fails?

scal开发者_JAVA技巧a> Float.floatToI

On pressing tab here, it displays Float.floatToIntBits. But,

scala> Float.floatToIntBits(2f)
<console>:6: error: value floatToIntBits is not a member of object Float
       Float.floatToIntBits(2f)
             ^


Float.floatToIntBits tries to call method on the object scala.runtime.Float (I think).

scala> Float
res2: Float.type = object scala.Float

You need java.lang.Float.floatToIntBits:

scala> java.lang.Float.floatToIntBits(2f)
res1: Int = 1073741824


The REPL code-completion shows methods from all the Float objects available on the path (i.e.scala.Float scala.runtime.Float and java.lang.Float). However scala.Float scala.runtime.Float takes precedence over java.lang.Float and hence the error.

The following works:

scala> import java.lang.{Float => JFloat}
import java.lang.{Float=>JFloat}

scala> JFloat.floatToIntBits(2f)
res5: Int = 1073741824
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜