什么时候需要Scala分号 [英] When are Scala Semicolons required

查看:211
本文介绍了什么时候需要Scala分号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被锁定的电脑困在工作中.但是我正在尝试练习我的Scala.我正在使用Ideone.com,因为我什至无法安装scalac ...

I am trapped at work with a locked down pc. But I am trying to practice my Scala. I am using Ideone.com since I can't even install 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编译器获得了以下消息:

I get this message back from the Ideone.com scala compiler:

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]).

但是,当我在语句的末尾添加分号时,就像这样:

However, when I add semicolons to the end of the statements, like this:

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中的后缀和后缀表示法是 AWESOME ,但是我肯定缺少一些东西.为什么Scala不将行的结尾视为语句的结尾?

I find the infix and postfix notation in Scala to be AWESOME, But I must be missing something. Why doesn't Scala consider the end of the line to be the end of the statement?

此博客上的第二个海报似乎有答案.斯卡拉人民应该接受这一点.如此烦恼...尽管这是第一个烦人的问题,但是我还是用这种漂亮的语言遇到的. http://www.scala-lang.org/node/4143

Second poster on this blog seems to have the answer. The Scala people should get on this. Such an annoyance.... Although this is about the first annoying this I've encountered in this otherwise beautiful language. http://www.scala-lang.org/node/4143

直接从文档中获得的另一种解释: http://docs.scala-lang.org/style/method-invocation. html

Another explanation straight from the documentation: http://docs.scala-lang.org/style/method-invocation.html

推荐答案

放置点时,Scala假定您正在使用arg0 operator arg1语法.类似于1 + 2之类的东西:Scala假定参数将跟随运算符.

When you drop the dot, Scala assumes that you are using arg0 operator arg1 syntax. It's the same with something like 1 + 2: Scala assumes that an argument is going to follow the operator.

因此,假设print是一个接受参数的方法,然后转到下一行寻找一个.您会收到一个错误,因为这实际上不起作用:print不接受参数.

So, it's assuming that print is a method that takes an argument, and it goes to the next line looking for one. You get an error, because that doesn't actually work: print doesn't take an argument.

通过添加分号,您就是告诉编译器该方法肯定不会有参数,因此它将停止寻找该参数.这可以帮助编译器弄清楚print不需要参数,所以一切都很好.

By adding the semicolon, you are telling the complier that there is definitely not going to be an argument to the method, so it will stop looking for one. This helps the compiler figure out that print doesn't need an argument, so all is fine.

要解决此问题,只需说pt0.print就可以了.

To fix the problem, just say pt0.print and you'll be fine.

这篇关于什么时候需要Scala分号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆