dimanche 26 juin 2022

Can we force all implementations to be singleton (objects)?

Let's say we have an abstract class (same question for traits also):

abstract class TypeParser[C <: Changes : TypeTag] extends Serializable {

  val enrichmentType: EnrichmentType
  protected def parseChanges(row: Row): C

}

Where implementations look like the following:

object PlaceholderParser extends TypeParser[PlaceholderChanges] {

  val enrichmentType: EnrichmentType = PlaceholderType
  override protected def parseChanges(row: Row): PlaceholderChanges = ???

}

The above implementation is a singleton, however, it can't be forced to be a singleton for future implementations. So one can simply implement it as a class, for example:

class PlaceholderParser2() extends TypeParser[PlaceholderChanges2] {

  val enrichmentType: EnrichmentType = PlaceholderType2
  override protected def parseChanges(row: Row): PlaceholderChanges2 = ???

}

Is there any way of forcing implementations to be a singleton?


side question: is there any advantage of forcing it?

Aucun commentaire:

Enregistrer un commentaire