How can I prevent some code repetition for a generic method in scala? My code only compiled when the type parameter was bound to the trait and not the method.
As you can see, I currently repeat the type parameters being passed to the trait and mehtod.
trait MyTrait[T <: Product, C <: OtherTrait] {
def provide(spark: SparkSession, c: C): Dataset[T]
}
object ConcreteProvider
extends MyTrait[MyCaseClass, MyCaseClassConfiguration] {
override def provide(
spark: SparkSession,
c: MyCaseClassConfiguration): Dataset[MyCaseClass] = {
spark.sql(s"SELECT field1, field2 FROM ${c.mydb}.${c.mytable}")
.as[MyCaseClass]
}
}
MyCaseClass
,MyCaseClassConfiguration
andCellFeedSubscriberConfiguration
, so I'm not sure what repetition you are concerned about. You don't show how the parameterc
is used insideprovide()
. You should be able factor out the body ofprovide
intoprovideHelper
and defineprovide
in the trait. – Bob Dalgleish Jun 19 '18 at 21:30c
. – Georg Heiler Jun 19 '18 at 22:05[T]
should be fine? – Georg Heiler Jun 19 '18 at 22:06