更正Scala 3语法,以提供来自高阶函数参数的给定



在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被弃用,givenusing被使用代替?我已经尝试用given代替implicit,但是编译器不喜欢那样。

myObj.doSomething { given x => // this fails!
...
}

这个地方仍然需要implicit关键字吗?

我想,正确的是

myObj.doSomething { ctx =>
given Context = ctx
...
}

myObj.doSomething { case ctx@given Context =>
...
}

允许模式中的given绑定

最新更新