Michael Burrows Michael Burrows - 11 days ago 5
Scala Question

How do I inherit constructors in Scala?

How can the following code be improved? I'm not keen on the casts in Subclass or the duplication between object Superclass and object Subclass

class Superclass {
val bitSet = BitSet.empty
def += (bitSet:BitSet) = { this.bitSet | bitSet ; this }
def += (ints:Array[Int]) = { ints.foreach { bitSet += _ } ; this }
override def toString = bitSet.toString
}

object Superclass {
def apply(bitSet:BitSet) = new Superclass += bitSet
def apply(ints:Array[Int]) = new Superclass += ints
}

class Subclass extends Superclass {
override def += (bitSet:BitSet) = (super.+= (bitSet)).asInstanceOf[Subclass]
override def += (ints:Array[Int]) = (super.+= (ints)).asInstanceOf[Subclass]
}

object Subclass {
def apply(bitSet:BitSet) = new Subclass += bitSet
def apply(ints:Array[Int]) = new Subclass += ints
}

object SubclassTest extends App {
println(Subclass(Array(1,2,3)))
}

Answer

Regarding cast, you can make your Superclass methods return the parent class' type:

  def += (bitSet: BitSet): this.type = { this.bitSet | bitSet; this }

Now, you don't need to override them:

  class Subclass extends Superclass
  val subclass: Subclass = new Subclass += BitSet.empty 

The returned value of += is of type Subclass.

It may be worth mentioning though, that your += method doen't do anything: BitSet.| does not modify the bitset, it just returns a copy, which you then promptly discard. The other one does work, but keeping mutable state around is generally frowned upon, unless you have a really, really good reason for it (which you most probably do not).

As for the duplication in apply, there isn't very much you can do about it generically. In this particular case, I would recommend to just get rid of it entirely, and make the bitSet a constructor parameter:

  class Superclass(val bitSet: BitSet = BitSet.empty) { 
   ...
  }
  class Subclass(bitSet: BitSet = BitSet.empty) 
    extends Superclass(bitSet)

Now, instead of apply, you can just do

  new Subclass(bitSet)
  new Superclass(bitSet)

or new Subclass() += ints new Subperclass() += ints