gpt4 book ai didi

Scala 未绑定(bind)占位符参数

转载 作者:行者123 更新时间:2023-12-04 17:23:16 26 4
gpt4 key购买 nike

我正在使用以下代码来满足我的需求:

 (1 to 5)..map(i => s"\\x${i}")  // Produces List("\\x1", "\\x2", "\\x3", "\\x4", "\\x5")

但我想使用占位符。根据 the string interpolator documentation :
(1 to 5).map(s"\\x${_}")

应扩展为:
(1 to 5).map(StringContext("\\\\x","").s(_))

但后者有效,前者无效,后者抛出 error: unbound placeholder parameter_ .为什么?

最佳答案

我相信语法:

(1 to 5).map(s"\\x${_}")

编译器认为_属于 s功能在这种情况下它将不起作用。您可以通过执行以下操作轻松解决此问题:
(1 to 5).map(i => s"\\x${i}")

你可能想看看这个 link进一步明确与匿名函数相关的占位符规则。

编辑:
根据这篇文章,占位符语法曾经有效,所以这可能是一个回归错误或一个从未打算以这种方式工作的功能: https://groups.google.com/forum/#!msg/scala-internals/G_54LGj0zpg/REZfyXZ6-RwJ

关于Scala 未绑定(bind)占位符参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16688365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com