【发布时间】:2014-08-03 09:41:38
【问题描述】:
class A {
def x(): Unit = {
println("tftf")
}
}
def t[A](x: A): Unit = {
x.x // <- error at this line
}
我得到编译错误 - 类型不匹配;找到:x.type(具有基础类型 A) 需要:?{def x: ?} 请注意,隐式转换不适用,因为它们不明确:在类型 [A](x: A)Ensuring[ 的对象 Predef 中的两个方法 any2Ensuring A] 和 [A](x: A)ArrowAssoc[A] 类型的对象 Predef 中的方法 any2ArrowAssoc 是从 x.type 到 ?{def x: ?} 的可能转换函数 - t
有人能用英语解释一下吗?我是 Scala 的新手。
【问题讨论】:
-
您可以将 A 作为类名,并使用
def t[T](x: A): Unit = ...之类的函数,如果这样可以清楚的话(除了 LimbSoup 答案)