为什么不能在字符串内插中使用_? [英] Why can't _ be used inside of string interpolation?

查看:141
本文介绍了为什么不能在字符串内插中使用_?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效

(x => s"$x")

但是这个

(s"${_}")

产生

[error] ...: unbound placeholder parameter
[error]   (s"${_}")

这是否是泄漏抽象的情况?

此外: (s"$_")失败,输出完全不同:

[error] ...: invalid string interpolation: `$$', `$'ident or `$'BlockExpr expected
[error]   (s"$_")
[error]      ^
[error] ...: unclosed string literal
[error]   (s"$_")

解决方案

在我看来,将字符串插值称为泄漏抽象是完全正确的.尽管它在大多数情况下都能正常工作,但在许多极端情况下,它并不能像人们期望的那样发挥作用.这是这种边缘情况的另一种体现.

我不知道为什么编译器不接受s"$_".前一段时间,有一个请求请求引入了用于模式匹配的这种语法: PR 2823

有趣的是,此PR还包含测试用例,用于测试下划线在外部模式匹配出现错误.

不幸的是,没有进一步的说明为什么以这种方式实现它.

Som Snytt ,实施PR的人对SO很活跃,希望他能提供更多信息. /p>

This works

(x => s"$x")

but this

(s"${_}")

results in

[error] ...: unbound placeholder parameter
[error]   (s"${_}")

Is this a case of leaky abstraction?

Furthermore: (s"$_") fails with a completely different output:

[error] ...: invalid string interpolation: `$$', `$'ident or `$'BlockExpr expected
[error]   (s"$_")
[error]      ^
[error] ...: unclosed string literal
[error]   (s"$_")

解决方案

Calling string interpolation a leaky abstraction is totally right in my opinion. While it works fine in most cases, there are many edge cases where it just doesn't work the way how one expects it. This one is another incarnation of such an edge case.

I don't know why s"$_" is not accepted by the compiler. Some time ago there were a pull request that introduced this syntax for pattern matching: PR 2823

Interestingly this PR also contains test cases that test that the underscore outside of a pattern match produces an error.

Unfortunately there is no further description why this is implemented the way it is implemented.

Som Snytt, the guy who implemented the PR is active on SO, hopefully he can tell more.

这篇关于为什么不能在字符串内插中使用_?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆