我有这个代码:
class Action[T]
class Insert[T] extends Action[T]
case class Quoted[T]()
implicit def unquote[T](q: Quoted[T]): T = {
throw new Exception("Success")
}
def test[A <: Action[_]](a: A): A = {
return a
}
try {
test[Insert[String]](Quoted[Insert[String]])
test(unquote(Quoted[Insert[String]]))
// test(Quoted[Insert[String]]) // does not compile
} catch {
case e: Exception => print(e.getMessage())
}
编译期间注释行失败:
error: inferred type arguments [ScalaFiddle.Quoted[ScalaFiddle.Insert[String]]] do not conform to method test's type parameter bounds [A <: ScalaFiddle.Action[_]]
test(Quoted[Insert[String]])
error: type mismatch;
found : ScalaFiddle.Quoted[ScalaFiddle.Insert[String]]
required: A
test(Quoted[Insert[String]])
有没有办法让它在不指定类型参数或像前两行那样显式使用转换函数的情况下编译?