隐式class不能应用于自类型?
Implicit class can't apply to self-type?
我定义了一个 class 和一个隐含的 class:
class User
implicit class RichUser(user: User) {
def hello = println("hello")
}
以下代码运行良好:
val user = new User
user.hello
但是下面的代码是不可编译的:
trait UserTrait {
this: User =>
this.hello // can't compile !!!
}
为什么不能编译,如何修复?
更新:
抱歉,它无法在 IntelliJ IDEA 中编译,我也没有尝试使用 scalac
。谢谢大家。
不太好,但您始终可以显式调用隐式(双关语)
trait UserTrait {
this: User =>
implicitly[RichUser](this).hello
}
我定义了一个 class 和一个隐含的 class:
class User
implicit class RichUser(user: User) {
def hello = println("hello")
}
以下代码运行良好:
val user = new User
user.hello
但是下面的代码是不可编译的:
trait UserTrait {
this: User =>
this.hello // can't compile !!!
}
为什么不能编译,如何修复?
更新:
抱歉,它无法在 IntelliJ IDEA 中编译,我也没有尝试使用 scalac
。谢谢大家。
不太好,但您始终可以显式调用隐式(双关语)
trait UserTrait {
this: User =>
implicitly[RichUser](this).hello
}