[英]What is the correct way to implement a generic method in scala
I have this Datasource trait我有这个数据源特征
trait DataSource {
def insert[T](foo: Foo): Either[Exception, Future[T]]
}
Then I create an implementation like:然后我创建一个实现,如:
class MongoDataSource(collection: MongoCollection[Document]) extends DataSource {
override def insert[ManagedObject](doc: ManagedObject): Either[Exception, Future[ManagedObject]] = {
Right(Future(new ManagedObject("")))
}
}
I got error:我收到错误:
class type required but ManagedObject found
Right(Future(new ManagedObject("")))
I think this might be what you want.我想这可能是你想要的。
trait DataSource[T] { //move type parameter to the trait
def insert(foo: T): Either[Exception, Future[T]]
}
class MongoDataSource(collection: MongoCollection[Document]) extends DataSource[ManagedObject] {
override def insert(doc: ManagedObject): Either[Exception, Future[ManagedObject]] = {
Right(Future(new ManagedObject("")))
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.