在A s scala 3中获得一个隐式参数化类型

发布于 2025-02-10 19:16:52 字数 921 浏览 0 评论 0原文

Scala 3语法问题。 我觉得围绕此内容还没有足够的文档。

当该类型被参数化时,如何在A for中获取隐式值?

for
  o               <- Option("line just to make for comprehension start")
  given Int       = 2
  a               = implicitly[Int] // error: no implicit argument of type Int was found for parameter e of method implicitly in object Predef
  given List[Int] = List(1, 2, 3)
  b               = implicitly[List[Int]] // error: no implicit argument of type List[Int] was found for parameter e of method implicitly in object Predef
yield ()

编辑:

,但是如果我在声明和用法之间插入虚拟行,则可以工作:

for
  o               <- Option("line just to make for comprehension start")
  given List[Int] = List(1, 2, 3)
  p               <- Option("dummy")
  b               = implicitly[List[Int]] // works
yield ()

A Scala 3 syntax question here.
I feel like there's not enough documentation around this yet.

How do you fetch an implicit value in a for comprehension, when its type is parameterized?

for
  o               <- Option("line just to make for comprehension start")
  given Int       = 2
  a               = implicitly[Int] // error: no implicit argument of type Int was found for parameter e of method implicitly in object Predef
  given List[Int] = List(1, 2, 3)
  b               = implicitly[List[Int]] // error: no implicit argument of type List[Int] was found for parameter e of method implicitly in object Predef
yield ()

Edit:

But if I insert a dummy line in between the declaration and the usage, it works:

for
  o               <- Option("line just to make for comprehension start")
  given List[Int] = List(1, 2, 3)
  p               <- Option("dummy")
  b               = implicitly[List[Int]] // works
yield ()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文