我有几个 DAO(使用Slick),如下所示
abstract class SuperRecord
abstract class SubSuperRecord extends SuperRecord
class Record1 extends SuperRecord
class Record2 extends SubSuperRecord
abstract class SuperTable[T <: SuperRecord] extends slick.driver.MySQLDriver.simple.Table[T]
abstract class SubSuperTable[T <: SubSuperRecord] extends SuperTable[T]
object DAO1 extends SuperTable[Record1]
object DAO2 extends SubSuperTable[Record2]
我在以下方法中返回这些 - 我没有声明返回类型,我让类型推理引擎来处理
def getTable(table: String) = {
table match {
case "DAO1" => DAO1
case "DAO2" => DAO2
case _ => throw new IllegalArgumentException("invalid table")
}
}
我正在尝试重构该方法以返回一个Try
对象,但这似乎使类型推理引擎感到困惑
def getTable(table: String) = Try {
table match {
case "DAO1" => DAO1
case "DAO2" => DAO2
case _ => throw new IllegalArgumentException("invalid table")
}
}
当我解压成功时,我得到了一个可序列化的,所以我试图帮助类型推理引擎
def getTable[T <: SuperRecord, U <: SuperTable[T]](table: String): Try[U] = Try {
table match {
case "DAO1" => DAO1
case "DAO2" => DAO2
case _ => throw new IllegalArgumentException("invalid table")
}
}
但是,编译器告诉我既不DAO1
也不DAO2
匹配这种类型。此方法的正确返回类型是什么?