11

Given the type-alias type Cal = java.util.Calendar how can the static getInstance method be accessed? I tried the following in Scala REPL:

scala> type Cal = java.util.Calendar
defined type alias Cal
scala> Cal.getInstance
<console>:8: error: not found: value Cal
          Cal.getInstance
          ^
scala> val Cal = java.util.Calendar
<console>:7: error: object Calendar is not a value
   val Cal = java.util.Calendar
                       ^

Is import java.util.{Calendar => Cal} followed by import Cal._ really my best bet?

Tim Friske
  • 1,897
  • 1
  • 15
  • 27
  • 2
    For the sake of consistency and to have the type-alias fully substitute the type I think Scala should also make static methods accessible through it. – Tim Friske Oct 07 '11 at 09:04

1 Answers1

12

You can't.

Yes, import java.util.{Calendar => Cal} is really your best bet.

Conceptually, Scala object members are similar to static members of Java classes. This might suggest the following would work, but it doesn't since there are actually no singleton objects available for Java classes.

scala> val Cal = java.util.Calendar
<console>:13: error: object Calendar is not a value
       val Cal = java.util.Calendar
                           ^
missingfaktor
  • 86,952
  • 56
  • 271
  • 360
  • I guess you could also just go ahead and wrap the whole thing manually in an `object Cal`. One might argue what solution is **the best bet**. – agilesteel Oct 06 '11 at 07:22