[英]How to make “this” implicit?
我有一個類實例應該隱式傳遞給這個類的方法。 像這樣的東西:
class Game(player: Player) {
protected implicit val implicitThis = this // This is the workaround I use now
def play = player.makeMove() // makeMove takes an implicit game: Game
}
您可以將其打包成特質。
trait ImplicitMe {
protected implicit def implicitMe: this.type = this
}
class Game extends ImplicitMe {
private def foo(implicit g: Game) = g
def bar = foo
}
(也可能想在特性上添加@inline
。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.