在Scala 2中我可以这样写:
// a function that needs an implicit context
def funcWithCtx(arg1: String)(implicit ctx: Context) = ???
myObj.doSomething { implicit ctx => // mark argument as the implicit context
funcWithCtx("arg1")
}
这个语法在Scala 3中工作,但我认为implicit
被弃用,given
using
被使用代替?我已经尝试用given
代替implicit
,但是编译器不喜欢那样。
myObj.doSomething { given x => // this fails!
...
}
这个地方仍然需要implicit
关键字吗?
我想,正确的是
myObj.doSomething { ctx =>
given Context = ctx
...
}
或
myObj.doSomething { case ctx@given Context =>
...
}
允许模式中的given
绑定