The best way to get a class of the object would probably be returning MyObject.getClass
from the block of code being compiled.
However, calling moduleSymbol
on the resulting class would result in NoSymbol
, because local objects aren't currently supported by Scala reflection. Why local objects? Because eval
wraps the code fed to it in a method to enable its evaluation, which means that MyObject
would end up being local to the wrapper method. Why aren't currently supported? Well, it's because Scala reflection relies on @ScalaSignature
annotations that carry Scala-specific metadata, and these annotations are currently not saved for local definitions.
I think it would be possible to change ToolBox.compile
(a non-evaluating counterpart of eval
) to special-case processing of top-level definitions, so that they not end up being compiled as local. Could you submit an issue to our issue tracker, so that we don't forget to fix it?