I need access to the class of the object, which is built in its own constructor (for various detailed reasons, which, it seems to me, are not relevant to my question).
I need something like this
class Foo(val i:Int) class Bar extends Foo(this.getClass.getName.length) val b = new Bar println(bi)
to print 3 ("Bar" .length). But this is not so. If the code above is inside some other object, "this" refers to this object. If the code above is not inside any other object (only in some package), the compiler complains about
error: this can be used only in a class, object, or template class Bar extends Foo(this.getClass.getName) ^
CLARIFICATION: I cannot change Foo to use val in my body instead of my constructor, because the Foo API already exists and is fixed (so yes, I have to be a constructor parameter). It needs an integer argument at the time of the constructor, but this integer can only be computed with access to the class.
(I know that the above example is still stupid and degenerate. If people care, I can explain in detail why I need a class in my real project, http://code.google.com/p/factorie )
Of course, the class of the created object is known to the compiler and the runtime at build time. What syntax can I get it with? (Is there such a syntax? If not, I wonder why. I'm surprised it doesn't seem like a simple, standard way to get this.)
source share