【问题标题】:Scala syntax - pass string to objectScala 语法 - 将字符串传递给对象
【发布时间】:2010-09-20 09:06:03
【问题描述】:

在使用 Scala 中的正则表达式时,我写了如下内容:

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

scala> y "Bar"

scala>

如您所见,第二个语句只是默默接受。这是合法的法律声明吗?如果是,它有什么作用?还是解析器中的一个错误,应该有错误消息?

【问题讨论】:

    标签: syntax scala notation


    【解决方案1】:

    这确实是解析器中的错误。它已在 scala 2.7.2(目前为 RC6)中修复

    $ ./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"
                 ^
    

    【讨论】:

      猜你喜欢
      • 2017-12-08
      • 2019-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 2019-01-02
      • 2013-01-21
      相关资源
      最近更新 更多