Search code examples
syntaxscalanotation

Scala syntax - pass string to object


While playing around with regexps in Scala I wrote something like this:

scala> val y = "Foo"
y: java.lang.String = Foo

scala> y "Bar"

scala>

As you can see, the second statement is just silently accepted. Is this legal a legal statement, and if so, what does it do? Or is it a bug in the parser and there should be an error message?


Solution

  • This is indeed an error in the parser. It is fixed in scala 2.7.2 (which is RC6 at the moment)

    $ ./scala
    Welcome to Scala version 2.7.2.RC6 (Java HotSpot(TM) Client VM, Java 1.5.0_16).
    Type in expressions to have them evaluated.
    Type :help for more information.
    scala> def y = "foo"
    y: java.lang.String
    
    scala> y "bar"
    <console>:1: error: ';' expected but string literal found.
           y "bar"
             ^
    
    scala> val x = "foo"
    x: java.lang.String = foo
    
    scala> x "foo"
    <console>:1: error: ';' expected but string literal found.
           x "foo"
             ^
    
    scala> "foo" "bar"
    <console>:1: error: ';' expected but string literal found.
           "foo" "bar"
                 ^