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
When you drop the dot, Scala assumes that you are using
arg0 operator arg1
syntax. It's the same with something like1 + 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.