This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Scala objects and their class names.

3 replies
Tomáš Heřman
Joined: 2011-09-28,
User offline. Last seen 42 years 45 weeks ago.
Hello again, guys!I have been thinking a little bit more about my scala keywords thing i posted earlier. I wanted to ask, could you, pretty please, point me to some document in which the generation of name of class of object (as in object a; a.getClass.getName) is described? Or better yet, can i always be sure that the class name has following format path.to.package.OutterClass$InnerClass$MyObject$? I only care about 'normal' objects, defined with object keyword (in case there is any other way to create object:)
I want to be sure that if i have object called x, i can always determine what exactly the name x is from getClass.getName. I hope i made myself clear ;).Thanks for any responses!
Ka Ter
Joined: 2011-10-05,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala objects and their class names.
Isn't there also a method called getSimpleName() in class Class? This should help.
Best Regards

Ka Ter

Am 30.10.2011 17:23, schrieb Tomáš Heřman:
Hello again, guys! I have been thinking a little bit more about my scala keywords thing i posted earlier. I wanted to ask, could you, pretty please, point me to some document in which the generation of name of class of object (as in object a; a.getClass.getName) is described? Or better yet, can i always be sure that the class name has following format path.to.package.OutterClass$InnerClass$MyObject$? I only care about 'normal' objects, defined with object keyword (in case there is any other way to create object:)
I want to be sure that if i have object called x, i can always determine what exactly the name x is from getClass.getName. I hope i made myself clear ;). Thanks for any responses!
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Scala objects and their class names.

On Sat, Nov 19, 2011 at 7:28 AM, Ka Ter wrote:
> Isn't there also a method called getSimpleName() in class Class? This should
> help.

No, you don't want that.

https://issues.scala-lang.org/browse/SI-2034
https://issues.scala-lang.org/browse/SI-4023

There are a bunch of closed tickets too. Basically getSimpleName
hardcodes the assumption that you mangle your nested classes exactly
the way java does, and throws an exception if that assumption is
invalidated. It remains an open question if and whether we will be
able to evolve name mangling to deal with java reflection's
undocumented requirements.

And that pretty well tells you the answer to the original question
too: we can't give you any guarantees about name mangling particulars
because there are still issues which may require its further
evolution. But at the moment I believe it works pretty much as you
expect, except when it doesn't. There are a bunch more tickets about
this, which you should read for further insight. There remain
difficult issues mostly arising from the challenge of having all of
one character '$' in which to reversibly encode the encyclopedia
britannica (while also keeping java happy.)

Florian Hars 3
Joined: 2011-05-08,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala objects and their class names.

On Sat, Nov 19, 2011 at 09:12:51AM -0800, Paul Phillips wrote:
> There remain
> difficult issues mostly arising from the challenge of having all of
> one character '$' in which to reversibly encode the encyclopedia
> britannica (while also keeping java happy.)

Just translate whatever you want to express into some formal
syntax and translate the ascii encoding of that into a unary
number. IIRC, there is a chapter in Gödel, Escher, Bach about
how to do someting like that.

Oh, you mean there is limit to the lenght of class names? Too
bad...

- Florian.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland