- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
String interpolation and anonymous functions
Wed, 2012-01-25, 11:33
Hi folks,
Under a previous incarnation of the String interpolation SIP some
rather cute function abstraction things were possible,
https://gist.github.com/a69d8ffbfe9f42e65fbf
Unfortunately this doesn't seem to be possible any more with the
current implementation,
scala> miles@lewis:scala$ scala-master -Xexperimental
Welcome to Scala version 2.10.0.rdev-4250-2012-01-25-gde2b0c6 (Java
HotSpot(TM) 64-Bit Server VM, Java 1.7.0_02).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val f : Int => String = "The there are "+_+" apples in the bowl" // OK
f: Int => String =
scala> val f : Int => String = s"The there are $_ apples in the bowl"
:1: error: invalid string interpolation
val f : Int => String = s"The there are $_ apples in the bowl"
^
scala> val f : Int => String = s"The there are ${_} apples in the bowl"
:1: error: unbound placeholder parameter
val f : Int => String = s"The there are ${_} apples in the bowl"
^
scala> val f : Int => String = n => s"The there are $n apples in the bowl" // OK
f: Int => String =
Is there any way we can make either (or prefereably both) of the two
failing cases work?
Cheers,
Miles