scala - 在scala中实现泛型方法的正确方法是什么
问题描述
我有这个数据源特征
trait DataSource {
def insert[T](foo: Foo): Either[Exception, Future[T]]
}
然后我创建一个实现,如:
class MongoDataSource(collection: MongoCollection[Document]) extends DataSource {
override def insert[ManagedObject](doc: ManagedObject): Either[Exception, Future[ManagedObject]] = {
Right(Future(new ManagedObject("")))
}
}
我得到错误:
class type required but ManagedObject found
Right(Future(new ManagedObject("")))
解决方案
我想这可能是你想要的。
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("")))
}
}
推荐阅读
- github-actions - 对 Github 操作运行端到端测试
- python - 如何在处理 HTTP 请求时使用 GetOldTweets3 获取给定主题的所有推文:HTTP 错误 429:请求过多
- flutter - 如何防止复选框在 Flutter 中过早调用 onChange?
- xml - 如何在 xslt 定义中转义大括号
- python - 如何从网站上抓取没有类或 id 的元素
- tfs - 我无法在集合或项目级别向任何 TFS 2018 安全组添加新成员
- apache-spark - 使用 pyspark 直接从 hdfs 加载带有 pandas 的大数据
- apache-kafka - 聚合和存储来自多个来源的数据
- django - 检查用户是否存在于数据库表中
- javascript - 让孩子使用父母传递的功能的问题