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?
CodePudding user response:
To our advantage all objects
extend an interface called Singleton
.
You can't extend it directly but we can use a Scala feature called self-types to enforce that all subtypes of TypeParser
to also be Singletons
(i.e. objects
)
abstract class TypeParser[C <: Changes : TypeTag] extends Serializable {
self: Singleton =>
...
}