Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use '_' in map for string interpolation

Tags:

scala

I have a List[String], and I want to quote each element. So I figured I could just do var.map(s"'$_'"). But that gives me error in interpolated string: identifier or block expected. What am I doing wrong? Is there a way to use the _ within a string interpolation?

I'm currently doing var.map("'" + _ + "'"), but it seems somewhat verbose and confusing.

like image 357
Soumya Avatar asked Dec 04 '22 04:12

Soumya


2 Answers

The PR to make this work wasn't merged because the syntax was considered too radical, too unsettling.

The PR for the same syntax in a pattern context was accepted, however.

There is some discussion at:

https://github.com/scala/scala/pull/2793

I would have used this syntax at least a handful of times. People like to save an extra arrow. People also don't like the burden of inventing identifiers needlessly. Yes, like x.

like image 140
som-snytt Avatar answered Dec 06 '22 19:12

som-snytt


I would do:

var.map(x => s"'$x'")

It is decidedly less "verbose and confusing", and probably less confusing than var.map(s"'$_'") as well.

like image 21
dhg Avatar answered Dec 06 '22 18:12

dhg