tags:

views:

86

answers:

1

When I try to omit dots from method invocations, like in this example program:

object Test extends Application {
  val baz = new Baz
  var foo = baz bar
  println(foo)
}

class Baz {
  def bar = "bar"
}

I get strange errors. The first one is error: recursive method foo needs type: println foo and the other one is error: type mismatch; found: Unit, required: Int, println(foo). The first error is in some strange way fixed if i specify that the type of foo should be String. The second one won't go away before I put a dot between baz and bar. What is the cause of this? Why does Scala think that baz bar is a recursive method?

+6  A: 

The problem you are seeing is that if you omit the dots the code is ambigous. The compiler will treat the expression as

var foo = baz.bar(println(foo))

thus foo is defined recursively and StringOps.apply method needs an Int argument (String will be implicitly converted to StringOps as String has no apply method).

You should only use the operator like syntax when calling methods that take one non-Unit argument to avoid such ambiguities.

Moritz
Thank you very much for the answer. I had no idea Scala would try to put lines together like that. I thought it treated line breaks as a semicolon. I experimented a bit, and the problem could also be solved by inserting a semicolon after `val foo = baz bar`.
mranders
Scala normally treats line breaks as whitespace but the compiler tries to infer semicolons where needed. In your case this just does not work well together with the type inference.
Moritz