tags:

views:

142

answers:

2
scala> 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)
             ^
+5  A: 

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
Alexey Romanov
Thanks. It works. curious to know why the method is displayed on available list of members while pressing tab in REPL?
Marimuthu Madasamy
There are some spurious things in the REPL auto-completion ... they are just bugs which will be fixed.
soc
+4  A: 

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
missingfaktor
Is the object `scala.Float` actually defined anywhere? Searching for `float` at the API site only gives `scala.runtime.Float`, nor is there a definition in `package.scala` or `Predef.scala`...
Alexey Romanov
@Alexey: It seems they moved `Float` from `scala` to `scala.runtime` in v2.8.
missingfaktor
@Alexey: I think `scala.Float` is now defined as an alias to `scala.runtime.Float` in the `scala` package object. http://paste.pocoo.org/show/253212/
missingfaktor
@Missing_Faktor Thanks.
Marimuthu Madasamy
Well, http://lampsvn.epfl.ch/trac/scala/browser/scala/tags/R_2_8_0_final/src/library/scala/package.scala doesn't seem to have it.
Alexey Romanov
@Alexey: The first line in the scaladoc of scala.runtime.Float(Link: scala-lang.org/api/current/scala/runtime/Float$.html) reads "A object representing object scala.Float." Not sure what that means. :(
missingfaktor
@Alexey: I don't know for sure but I think it's like this: Neither the `scala.runtime.Float` object nor its alias in `scala` package object actually exist; They are just compiler magic.
missingfaktor
`scala.runtime.Float` _is_ defined in http://lampsvn.epfl.ch/trac/scala/browser/scala/tags/R_2_8_0_final/src//library/scala/runtime/AnyValCompanion.scala#L77 but "compiler magic" does seem likely for `scala.Float`.
Alexey Romanov
wish Scala REPL lists only the methods on the object which has 'precedence' because it seemed so obvious to work but the listed method resulting in error :(
Marimuthu Madasamy
@Alexey: But the alias would be trivial to define: `val Float = scala.runtime.Float`. Why won't they define it then?
missingfaktor
The following changeset seems to be related to the compiler magic: https://lampsvn.epfl.ch/trac/scala/changeset/21408 . I have neither found a reference in the specification nor the corresponding ticket, but I haven't searched very thoroughly.
mkneissl