开发者

; expected but <place your favourite keyword here> found

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 error开发者_Go百科s? Where should I find them? are there some common syntactic errors elsewhere when this happens?


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


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


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.


I get this error when I forget to put an = sign after a function definition:

def function(val: String):Boolean {
   // Some stuff
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜