tags:

views:

128

answers:

3

Hello everyone, I'm trying to write a class for a scala project and I get this error in multiple places with keywords such as class, def, while.

It happens in places like this:

var continue = true
while (continue) {
    [..]
}

And I'm sure the error is not there since when I isolate that code in another class it doesn't give me any error.

Could you please give me a rule of thumb for such errors? Where should I find them? are there some common syntactic errors elsewhere when this happens?

+1  A: 

It sounds like you're using reserved keywords as variable names. "Continue", for instance, is a Java keyword.

TMN
Im not sure this is the problem: for instance this is a perfectly correct piece of code in scala:class Test { def method { var continue = true while (continue) { continue = false } }}
hoheinzollern
`continue` is not reserved in Scala, and moving code with a reserved Scala keyword will not make it work in another context.
Rex Kerr
+1  A: 

You probably don't have parentheses or braces matched somewhere, and the compiler can't tell until it hits a structure that looks like the one you showed.

The other possibility is that Scala sometimes has trouble distinguishing between the end of a statement with a new one on the next line, and a multi-line statement. In that case, just drop the ; at the end of the first line and see if the compiler's happy. (This doesn't seem like it fits your case, as Scala should be able to tell that nothing should come after true, and that you're done assigning a variable.)

Rex Kerr
I tried it, but when I drop the first ; then the error goes to the next statement. It seems to be something strange before the error, but I checked all the parentheses (and put some where the syntax could have been misleading, like if and else with just one statement) and they seem to match.
hoheinzollern
+1  A: 

Can you let us know what this code is inside? Scala expects "expressions" i.e. things that resolve to a particular value/type. In the case of "var continue = true", this does not evaluate to a value, so it cannot be at the end of an expression (i.e. inside an if-expression or match-expression or function block).

i.e.

def foo() = {
  var continue = true
  while (continue) {
    [..]
  }
}

This is a problem, as the function block is an expression and needs to have an (ignored?) return value, i.e.

def foo() = {
  var continue = true
  while (continue) {
    [..]
  }
  ()
}

() => a value representing the "Unit" type.

jsuereth
@jsuereth: In Scala 2.8, a local definition (`val` or `var` or `def`) *may* appear last in a block, rendering the type of that block `Unit`. In 2.7 this was forbidden.
Randall Schulz