I created a class A that extends class B, which takes two parameters.
When I create a class object of A using reflection and passing two parameters, an object is create w/o any exception, but the two "constructor" parameters do not contain the values I passed. Is that the intended behaviour or am I doing something wrong?
Sample code:
class B (p1: Int = 0, p2: String = "") { ... } class A extends B { ... } val mirror = universe.runtimeMirror(getClass.getClassLoader) val classSymbol = mirror.classSymbol(Class.forName("package.A")) val constructor = mirror.reflectClass(classSymbol).reflectConstructor( classSymbol.toType.decl(universe.termNames.CONSTRUCTOR).asMethod) val object: B = constructor(1, "C").asInstanceOf[B] "object" contains an instance of A, but with p1 = 0 and p2 = "". I expected it to contain p1 = 1 and p2 = "C".
If I move (p1: Int = 0, p2: String = "") to A it works as expected.
Is that the normal behaviour and my expectations were just wrong, or is there a mistake in my code?
A extends Bgiven the implementation that you provided, you have to provide constructor parameters forB.Adoesn't even take any arguments. If you tried to do this without reflection, you would get a compile error. Not sure why you get default values instead of a runtime error.