Why does Scala's semicolon inference fail here?

前端 未结 3 1693
旧巷少年郎
旧巷少年郎 2020-12-09 10:09

On compiling the following code with Scala 2.7.3,

package spoj

object Prime1 {
  def main(args: Array[String]) {
    def isPrime(n: Int) = (n != 1) &&am         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 10:41

    Is it because scala is assuming that you are using the syntax a foo b (equivalent to a.foo(b)) in your call to readInt. That is, it assumes that the while loop is the argument to readInt (recall that every expression has a type) and hence the last statement is a declaration:

    var ntests = read nextInt x
    

    wherex is your while block.

    I must say that, as a point of preference, I've now returned to using the usual a.foo(b) syntax over a foo b unless specifically working with a DSL which was designed with that use in mind (like actors' a ! b). It makes things much clearer in general and you don't get bitten by weird stuff like this!

提交回复
热议问题