By not using the declaration, the type of x is Array[Nothing], and it lacks the `withFilter` attribute.
C:\Users\bert>scala
Welcome to Scala version 2.9.2 (Java HotSpot(TM) Client VM, Java 1.7.0_06).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val x = Array(1, 2, 3)
x: Array[Int] = Array(1, 2, 3)
scala>
You can see that scala infers the type right (Array[Int]). scala> for (y<-x if x > 2)
| println(y)
<console>:9: error: value > is not a member of Array[Int]
for (y<-x if x > 2)
^
As you suggested, there may be a idiomatically better way to get implicit inference and also use the filters. I would be glad if some one corrects me on that.Edit: Yeah, I see the silly error I made. I'll better get some sleep. I will leave this comment intact, for the responses to make sense.
for (y <- x if y > 2) ...I see that the recently released IDEA 12 has a similar feature (but so far incomplete and undocumented according to their bug tracker).