3

I want to use the static method Integer#bitCount(int). But I found out that I'm unable to use a type alias does to achieve it. What is the difference between a type alias an an import alias ?

scala> import java.lang.{Integer => JavaInteger}
import java.lang.{Integer=>JavaInteger}

scala> JavaInteger.bitCount(2)
res16: Int = 1

scala> type F = java.lang.Integer
defined type alias F

scala> F.bitCount(2)
<console>:7: error: not found: value F
       F.bitCount(2)
       ^
Frankie Ribery
  • 11,933
  • 14
  • 50
  • 64
  • 5
    This recent question may help: http://stackoverflow.com/questions/5031640/what-is-the-difference-between-a-class-and-a-type-in-scala-and-java. – huynhjl Feb 25 '11 at 07:48
  • If you want to call it `F`, why not import it as `{Integer => F}`? – Rex Kerr Feb 25 '11 at 17:20

4 Answers4

7

In Scala, instead of using static method, it has companion singleton object.

The companion singleton object's type is different to the companion class, and the type alias is bound with class, not the singleton object.

For example, you may have the following code:

class MyClass {
    val x = 3;
}

object MyClass {
    val y = 10;
}

type C = MyClass // now C is "class MyClass", not "object MyClass"
val myClass: C = new MyClass() // Correct
val myClassY = MyClass.y // Correct, MyClass is the "object MyClass", so it has a member called y.
val myClassY2 = C.y // Error, because C is a type, not a singleton object.
Brian Hsu
  • 8,781
  • 3
  • 47
  • 59
  • So by doing an import, scala is bringing in the class & automagically the companion object ? – Frankie Ribery Feb 25 '11 at 12:30
  • No, C is not "class MyClass." C is a type, not a class. You could also write type C = List[MyClass], but List[MyClass] is not a class. It's a type. – James Iry Feb 25 '11 at 15:40
3

You cannot do that because F is a type, and not an object, and has therefore no static member. More generally, there is no static member in Scala: you need to implement those in a singleton object that sort of represents the "static component" of the class.

As a result, in your case, you need to refer directly to the Java class so that Scala is aware that it may contain static members.

Greg
  • 6,038
  • 4
  • 22
  • 37
3

You could create a short cut to the static java method like this

val bitCount:(Int) => Int = java.lang.Integer.bitCount
sellmerfud
  • 340
  • 2
  • 10
2

F is a static type, it's not an object and it's not a class. In Scala you can only send messages to objects.

class MyClass  // MyClass is both a class and a type, it's a class because it's a template for objects and it's a type because we can use "MyClass" in type position to limit the shape of computations

type A = MyClass  // A is a type, even if it looks like a class.  You know it's a type and not a class because when you write "new A.getClass" what you get back is MyClass. The "new" operator takes a type, not a class.  E.g. "new List[MyClass]" works but "new List" does not.

type B = List[MyClass] // B is a type because List[MyClass] is not a class

type C = List[_ <: MyClass] // C is a type because List[_ <: MyClass] is clearly not a class

What is the difference between a class and a type in Scala (and Java)?

Community
  • 1
  • 1
James Iry
  • 19,367
  • 3
  • 64
  • 56