3

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

Anyway this is not compiling:

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
  }
}

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;
  }
}

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?


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

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

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
matt walters
  • 591
  • 6
  • 14

1 Answers1

8

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.

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.

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.

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

dhg
  • 52,383
  • 8
  • 123
  • 144
  • Additionally, you can put in a blank line after the `pt0 print` and it will interpret it as the end of that particular statement, like `;` does. – wkl May 18 '12 at 17:08
  • So scala assumes that when I drop the dot I am using infix notation even though print takes 0 params? Why can't scala figure out the difference between infix and postfix depending on the araity of the method? I am reading Programming Scala by Wampler and Payne, and there is no mention of this. The example is an isolated line of scala code: "List(1, 2, 3) size" There is no mention of a ; or \n\n required.... – matt walters May 18 '12 at 17:09
  • 2
    @mattwalters The pronouncement was made over Twitter last year; I am surprised you missed it: http://twitter.com/#!/odersky/statuses/49882758968905728 – Luigi Plinge May 18 '12 at 17:52
  • Scala can't use types to determine what to do at that point, it has to be able to unambiguously parse before it can determine which method to call. – Dan Shryock May 18 '12 at 19:52