I'm coding against an API that gives me access to remote file system. The API returns a list of files and directories as list of node objects (parent to file and directory).
I want to work only on a directories, ignoring files. I've tried to use type pattern matching in for
loop but it does not work:
for {
dir: CSDir <- workarea.getChildren() // <-- I'm getting an error here complaining about type conversion
} {
println(dir)
}
Here is a similar example using scala basic objects to run it without dependencies:
val listOfBaseObjects:List[Any] = List[Any]("a string", 1:Integer);
for (x: String <- listOfObjects) {
println(x)
}
I end up using a regular pattern matching in side of for loop and that works fine:
// This works fien
for (child <- workarea.getChildren()) {
child match {
case dir: CSDir => println(dir)
case _ => println("do not nothing")
}
}
Question:
Can you tell me why the first /second example does not work in scala 1.9?
In the "Programming in Scala" the for
loop is advertised to use the same pattern matching as the match
so it should work.
If the for and match are different it would be great if you could point me to some articles with more details. What about pattern matching in assignment?
Update:
I can't accept an answer that states that it is impossible to skip elements in for loop as this contradicts with the "Prog. in scala". Here is a fragment from section 23.1:
pat <- expr
... The pattern pat
gets matched one-by-one against all elements of that list. ... if the match fails, no MatchError is thrown. Instead, the element is simply discarded from the iteration
and indeed the following example works just fine:
scala> val list = List( (1,2), 1, 3, (3,4))
scala> for ((x,y) <- list) { println (x +","+ y) }
1,2
3,4
Why then type matching does not work?
See Question&Answers more detail:
os