Is it possible to inherit auxiliary constructors in Scala? -


I wonder if it is possible to inherit assistant constructors in Scala?

I have tested this code, and it has complained

temp.scala: 18: error: too much logic for constructor child: () Children

val a = new child (42)

^ ^
 (V: int) = {this (set) set value (V)}} class child: 
{0} value class = {value}: int = 0 protected def setValue (v: int) = {value = v} Father {} increases the value of a new child (42)

but if I

  protected def it (v: int) = {( ) SetValue (v)}  

In the child's class, everything is fine.

Not at all, and your example shows why depending on the exact constructor you used You have either started an abstract variable value or have not started.

This is a potential source for many problems, and so Scala has decided that a consistent initialization to ensure that all object creation is eventually directed through the primary constructor.

If you want value to be a default value, you can specify it as a default parameter (2.8 +):

  Abstract class father (value value: int = 0)  

Or you can use auxiliary constructor to achieve the same effect in Scala 2.7:

  Abstract class father (val value: int) {def () = this (0)}  

with father above Define, child's following definitions Both are valid:

  class child (v: int) father (v) extends expansion of class papa ()  

You too < Code> value can be a Var if you mean

if the semantics of value is not valid that it has been initially started, The correct scala idioms are to declare it as option [int] :

  abstract class father (value value: option [int] = some (0))  

Comments