16

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"$_")
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111

1 Answers1

7

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.

Community
  • 1
  • 1
kiritsuku
  • 52,967
  • 18
  • 114
  • 136
  • Thanks, and looking forward to his comment then :) btw I think the `_` in pattern matching and `_` in lambda function syntax are 2 different things, but nevertheless good to know that it works in pattern matching! – Erik Kaplun Nov 11 '13 at 00:04
  • also btw `s"$_"` fails with a different error than `s"${_}"`. – Erik Kaplun Nov 11 '13 at 00:07
  • 2
    I don't know why ping in a comment didn't work. The PR for s"$_" was https://github.com/scala/scala/pull/2793 and you can read the cold feet there. The feature has history, and I think it was really neat. I'm sorry people are confused by _ and function lits, but that's what SO is for. I have wanted ss map s"text $_" a couple of times. – som-snytt Nov 28 '13 at 23:09
  • @som-snytt: Sometimes a little bit convincibility is necessary or just more time - who knows maybe in future the chances are higher to get that thing merged. – kiritsuku Nov 28 '13 at 23:45
  • 3
    I think the reasoning that `s"$_"` would make the language more complex is unjustified... the fact that `_` currently doesn't work uniformly where you'd expect it to work is complexity. – Erik Kaplun Mar 03 '14 at 04:25