Character names for string interpolation

To my surprise, this will not work -

implicit class LambdaContext(val sc: StringContext) extends AnyVal { def λ(args: Any*) = args } scala> λ"λx.x" <console>:1: error: ';' expected but string literal found. λ"λx.x" 

Although this is a valid method name:

 scala> new LambdaContext(new StringContext("λx.x")).λ() res1: Seq[Any] = List() 

Not sure if this is a mistake or a simple restriction.

+5
source share
1 answer

I'm not sure, but when you use λ in λ"λx.x" , this seems to be considered an operator, and only printable ASCII characters are allowed for operators. So, if you replace λ with something less weird, like l or lambda , it works.

 implicit class LambdaContext(val sc: StringContext) extends AnyVal { def l(args: Any*) = λ(args) def λ(args: Any*) = args } scala> l"lx.x" res1: Seq[Any] = List() 
0
source

All Articles