Scala从泛型类型实例化一个具体类
我有一个共性,是这样的:Scala从泛型类型实例化一个具体类,scala,generics,Scala,Generics,我有一个共性,是这样的: trait MyTrait[T] { def doSomething(elems: Seq[T]) } object MyTraitFactory { def apply[T](param1: Boolean, param2: Boolean): MyTrait[T] = { // based on the type of T, I would like to instantiate sub types } } 然后我有一个对象工厂,其定义如下:
trait MyTrait[T] {
def doSomething(elems: Seq[T])
}
object MyTraitFactory {
def apply[T](param1: Boolean, param2: Boolean): MyTrait[T] = {
// based on the type of T, I would like to instantiate sub types
}
}
然后我有一个对象工厂,其定义如下:
trait MyTrait[T] {
def doSomething(elems: Seq[T])
}
object MyTraitFactory {
def apply[T](param1: Boolean, param2: Boolean): MyTrait[T] = {
// based on the type of T, I would like to instantiate sub types
}
}
我介绍了一些具体的实现,例如:
class MyStringTrait extends MyTrait[String]
class MyIntTrait extends MyTrait[Int]
现在,我需要在对象工厂中查找类型并实例化相应实现的魔法位。有什么建议吗?这可以在scala中使用隐式类型类解决。为每种类型创建具有具体实现的factory特性:
object MyTraitFactory {
def apply[T](param1: Boolean, param2: Boolean)(implicit factory: MyTraitCreator[T]): MyTrait[T] = {
// call the typeclass create method
factory.create(param1, param2)
}
// factory trait
trait MyTraitCreator[T] {
def create(param1: Boolean, param2: Boolean): MyTrait[T]
}
// provide an implicit factory object for the specific types:
object MyTraitCreator {
implicit object MyStringTraitCreator extends MyTraitCreator[String] {
override def create(param1: Boolean, param2: Boolean): MyTrait[String] = {
// create the String type here
new MyStringTrait
}
}
implicit object MyIntTraitCreator extends MyTraitCreator[Int] {
override def create(param1: Boolean, param2: Boolean): MyTrait[Int] = {
// create the Int type here
new MyIntTrait
}
}
}
}
Scala使用隐式参数“隐藏”typeclass。但要使其工作,您必须确保将隐式工厂对象保留在编译器查找隐式对象的某个位置(例如,MyTraitCreator
的伴生对象,如上所述)。模式在没有隐式
的情况下也能正常工作,但是需要调用方在每次调用时提供具体的工厂
此解决方案包含大量锅炉板代码,但在编译时静态工作,并且不受类型擦除的影响。它甚至在scala中附带了语法糖:
def apply[T: MyTraitCreator](param1: Boolean, param2: Boolean): MyTrait[T] = {
// call the typeclass create method
implicitly[MyTraitCreator[T]].factory.create(param1, param2)
}