Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
何时需要Scala分号_Scala_Jvm_Postfix Notation - Fatal编程技术网

何时需要Scala分号

何时需要Scala分号,scala,jvm,postfix-notation,Scala,Jvm,Postfix Notation,我在工作中被一台锁着的电脑困住了。但我正在尝试练习我的Scala。我正在使用Ideone.com,因为我甚至不能安装scalac 无论如何,这不是编译: class DPt(var name: String, var x: Double, var y: Double){ def print = println("DPt; name: " + name + " x: " + x + " y: " + y) } object Main { def main(args: Ar

我在工作中被一台锁着的电脑困住了。但我正在尝试练习我的Scala。我正在使用Ideone.com,因为我甚至不能安装
scalac

无论如何,这不是编译:

class DPt(var name: String, var x: Double, var y: Double){

        def print = println("DPt; name: " + name + " x: " + x + " y: " + y)
}


object Main {
  def main(args: Array[String]) {
        val pt0 = new DPt("Joe", 1.0, 1.0)
        println("checking generated accessor: " + pt0.x)
        pt0 print
        pt0.x_=(3.0)
        pt0 print
  }
}
我从Ideone.com scala编译器中得到以下消息:

Main.scala:12: error: Unit does not take parameters
    pt0 print
            ^
one error found
spoj: The program compiled successfully, but Main.class was not found.
      Class Main should contain method: def main(args: Array[String]).
但是,当我在语句末尾添加分号时,如下所示:

class DPt(var name: String, var x: Double, var y: Double){

        def print = println("DPt; name: " + name + " x: " + x + " y: " + y)
}


object Main {
  def main(args: Array[String]) {
        val pt0 = new DPt("Joe", 1.0, 1.0);
        println("checking generated accessor: " + pt0.x);
        pt0 print;
        pt0.x_=(3.0);
        pt0 print;
  }
}
我发现Scala中的中缀和后缀符号非常棒,但我肯定遗漏了一些东西。为什么斯卡拉不认为这句话的结尾是陈述的结尾?
这个博客上的第二张海报似乎有答案。Scala的人应该参与进来。真讨厌。。。。虽然这是我在这门漂亮的语言中遇到的第一个恼人的问题。

另一个直接来自文档的解释:

当您删除点时,Scala假定您使用的是
arg0运算符arg1
语法。类似于
1+2
:Scala假设一个参数将跟随运算符

因此,它假设
print
是一个接受参数的方法,并转到下一行查找参数。您会得到一个错误,因为这实际上不起作用:
print
不接受参数

通过添加分号,您告诉编译器该方法肯定不会有参数,因此它将停止寻找参数。这有助于编译器了解
print
不需要参数,因此一切正常


要解决这个问题,只需说
pt0.print
,你就没事了。

此外,你可以在
pt0 print
后面加一个空行,它将解释为该特定语句的结尾,如
是。所以scala假设当我删除点时,我使用中缀符号,即使打印需要0个参数?为什么scala不能根据方法的多样性来区分中缀和后缀呢?我正在阅读Wampler和Payne编写的Scala编程,没有提到这一点。这个例子是一行孤立的scala代码:“列表(1,2,3)大小”没有提到;或者\n\n需要……@mattworts该声明是去年在Twitter上发布的;我很惊讶你错过了它:Scala不能使用类型来决定在那个时候做什么,它必须能够明确地解析,然后才能决定调用哪个方法。使用而不是IDE方法。