Search code examples
scala

Scala compile fails because of whitespace on a negative literal assignment?


This code fails to compile with scala 2.13:

case class Foo(bar: String, baz: Int = -1)
val foo = Foo("foo", baz=-2)

Says Not found value baz. This looks weird, doesn't it? Somehow, I have never encountered it before. Just wondering if someone knows of a reason why this should not be working? Or is it just a compiler bug, as basic as it looks?

Works fine if you add whitespace around (or even just after) the = ... or if you use anything that's not a negative literal constant instead of -2


Solution

  • Since scala supports custom operators with symbols like = and -, the compiler can't make a distinction between the following possible choices:

    • baz =- 2 --> baz.=-(2)
    • baz = -2 --> baz.=(-2)

    Spaces can be used to make this unambiguous.