- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Accessing the instances of Scala objects from Java code
Mon, 2010-09-20, 21:44
Hey folks,
I filed http://lampsvn.epfl.ch/trac/scala/ticket/3846 last week and Tiark rightly pointed out that it deserves wider discussion on the list.
To sum up, calling *methods* on Scala objects from Java is easy and idiomatic thanks to the static forwarder methods. However, when you want to access the *instance* of the object itself, you need to do this ugly dance:
Foo$.MODULE$
My usual workaround is:
object Foo { def it = this}
So now, in Java, I can just say Foo.it() and everybody's happy--at least as happy as a Scala programmer coding in Java can be. :)
I think a lot of people would benefit if a similar method were generated for us on all Scala objects. "it" is probably not an ideal name, "instance" might be a reasonable choice, or even "getInstance" if we really want to pander. ;)
TIA for your comments!
-0xe1a
I filed http://lampsvn.epfl.ch/trac/scala/ticket/3846 last week and Tiark rightly pointed out that it deserves wider discussion on the list.
To sum up, calling *methods* on Scala objects from Java is easy and idiomatic thanks to the static forwarder methods. However, when you want to access the *instance* of the object itself, you need to do this ugly dance:
Foo$.MODULE$
My usual workaround is:
object Foo { def it = this}
So now, in Java, I can just say Foo.it() and everybody's happy--at least as happy as a Scala programmer coding in Java can be. :)
I think a lot of people would benefit if a similar method were generated for us on all Scala objects. "it" is probably not an ideal name, "instance" might be a reasonable choice, or even "getInstance" if we really want to pander. ;)
TIA for your comments!
-0xe1a
Daniel
On Mon, Sep 20, 2010 at 3:44 PM, Alex Cruise <alex@cluonflux.com> wrote: