score:1
let's first take reflection out of the picture completely, and just try to do what you are doing without using reflection:
class b(p1: int = 0, p2: string = "")
class a extends b
//val b = (new a(1, "c")).asinstanceof[b]
// even simpler:
val b = new a(1, "c")
// error: too many arguments for constructor a: ()a
// new a(1, "c")
// ^
as you can see, it doesn't even compile. why? well, you defined a
's constructor to take no arguments! somehow, during the reflection process, this information gets lost and you end with a legal call passing arguments to a constructor that doesn't take any and just ignores them.
score:1
i don't know the underlying reason, but it probably has to do with some quirk in how reflection works at runtime.
in any case this kind of code will get you what you want (without repeating the default values specified in the superclass). note that you should explicitly specify the parameters from the superclass you are using in the subclass:
class b (p1: int = 0, p2: string = "") {
override def tostring: string = s"p1 = $p1, p2 = $p2"
}
class a (p1: int, p2: string) extends b (p1, p2) { }
...
val obj: b = constructor(1, "c").asinstanceof[b]
println(obj)
output:
p1 = 1, p2 = c
Source: stackoverflow.com
Related Query
- Scala reflection - constructor not called
- Scala Child Class Constructor is not being called from Java Parent Class
- In Scala static value initialization does not appear to be happening before the main method is called
- Wrong number of arguments invoking a Scala constructor using reflection
- Main method is not called in Scala script
- Scala reflection with Serialization (over Spark) - Symbols not serializable
- Why are implicit variables not initialized in Scala when called from unit test?
- Alternate constructor on Scala case class not defined: not enough arguments for method
- Scala - Why are overloaded methods not called based on runtime class?
- Scala - method not recognized in constructor
- Public Scala constructor fields not public in Java
- Basic Scala reflection code using recursive types does not compile. Why ? How can it be fixed?
- Scala Reflection: How to find a no-arg constructor via reflection (if there are multiple constructors) ? What is the type signature of "no-arg"?
- Scala constructor overloading parameter is not taking effect
- scala reflection can not work at this case
- Determining constructor parameter count and type via reflection in Scala
- "Too many arguments" error in Scala superclass constructor but not in REPL
- Scala is not creating private constructor for companion class
- scala abstract class: Not able to access constructor paramater
- How can I use Scala reflection to get only primary constructor parameters?
- Scala error: X value is not a member of Y --> property not visible despite being declared in default constructor
- Base Class constructor not being called in JFXApp
- Cannot use primary constructor in Scala method: type "does not take parameters"
- How to invoke default constructor using scala reflection if we have default value defined in our constructor
- Calling a constructor through java reflection in scala 2.10
- Passing class name to super class constructor in Scala via reflection
- cp command does not find files when called from a Scala Process
- Get constructor parameter values using scala reflection
- scala tail recursion decorator not letting tail recursive function to be called
- Scala Future is not properly called when using implicit class
More Query from same tag
- split strings in array and convert to map
- Endpoint requires authentication Rest API Spray
- How can I get the build output directory inside tests run by sbt?
- Spark SQL with Scala : deprecation warning for registerTempTable
- From R to scala: Importing Libraries
- Scala in Java code using Spring without Default Constructor
- Set changing scala project dependency in Intellij IDEA 14 (for code completion and inspection)
- Scala generic: type mismatch
- How to implement Future as Applicative in Scala?
- Scala constructor signature
- Unexpected behaviour overriding multiple attributes in an object
- sbt won't install correctly; homebrew changing java versions
- In Scala Timestamp of now plus 5 minutes
- How can I call a method for these vectors inside a loop?
- overriding classes using sbt console/spark-shell
- Spark Job status logging
- overloaded method value apply with alternatives
- Ordering of implicit method and mapping with shapeless
- Get a message out of an Akka Actor
- How to pass class parameter to saveAsSequenceFile() in scala
- Can I define a table's ROW_FORMAT with Slick's Table class?
- Will The Thread Stop in This Promise Computation
- Scala synchronization best practice
- How to share .proto (protobuf) files using a shared scala library using sbt
- how to filter and extract case class objects from big flat csv files using shapeless?
- Scala Play 2.3 framework Json validate recursively
- fs2 - Sharing a Ref with 2 streams
- Efficiently mapping members to their owning structure
- Is there any similar filter concept of twitter finagle in play framework
- How to to run spark cluster on ec2 from sbt?