本文介绍了为什么scala的模式加工不适用于类型匹配的for循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我编写一个API,让我访问远程文件系统。 API返回文件和目录的列表作为节点对象列表(父文件和目录)。 我只想在目录上工作,忽略文件。我试图在中使用类型模式匹配循环,但它不工作: for { dir:CSDir< - workarea.getChildren()//< - 我收到一个错误,抱怨类型转换} { println (dir)} 这里是一个类似的例子,使用scala基本对象来运行它依赖关系: val listOfBaseObjects:List [Any] = List [Any](a string,1:Integer); for(x:String< - listOfObjects){ println(x)} 我最终在for循环中使用常规模式匹配,并且工作正常: // this work fien for(child< - workarea.getChildren()){ child match { case dir:CSDir => println(dir) case _ => println(do not nothing)} } 问题: / h2> 你能告诉我为什么第一个/第二个例子在scala 1.9中不起作用吗? 在Scala中, for 循环被宣告使用与 match 相同的模式匹配,所以它应该工作。 p> 如果for和match不同,这将是非常好的,如果你可以指向我的一些文章与更多的细节。 更新: 我不能接受一个答案是不可能在for循环中跳过元素,因为这与Prog。in scala矛盾。这是第23.1节的一个片段:,确实下面的示例工作正常: scala> val list = List((1,2),1,3,(3,4)) scala> for((x,y) 1,2 3,4 / pre> 为什么类型匹配不起作用?解决方案这是长期存在的问题900 ,之前已经讨论过很多次。常见的解决方法是使用类似于 for(y @(_ y:String)< - listOfBaseObjects){ println(y)} Jason Zaugg提供了更好的版本 object typed {def unapply [A](a:A)= Some(a) } for(typed(y:String)< - listOfBaseObjects){ println(y)} 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 fienfor (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: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,23,4Why then type matching does not work? 解决方案 This is the long-standing issue 900 and has been discussed many times before. The common workaround is to use something like:for (y@(_y:String) <- listOfBaseObjects) { println(y)}A nicer version is provided by Jason Zaugg in the comments to the above-mentioned ticket:object Typed { def unapply[A](a: A) = Some(a) }for (Typed(y : String) <- listOfBaseObjects) { println(y)} 这篇关于为什么scala的模式加工不适用于类型匹配的for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 00:54