3
A
回答
5
是的。
scala> val x = 33
x: Int = 33
scala> val xml = <foo> { x } </foo>
xml: scala.xml.Elem = <foo> 33 </foo>
scala> xml match { case <foo> { x } </foo> => 42 }
res0: Int = 42
我想在您的混淆的来源是不匹配对<foo> 33 </foo>
scala> <foo> 33 </foo> match { case <foo> { x } </foo> => 42 }
scala.MatchError: <foo> 33 </foo> (of class scala.xml.Elem)
这是因为当你的空间多余的元素前后{}
使用{}
,插入阶,所以你得到三个元素,而不是一个。在第二个例子中
scala> scala.xml.Elem.unapplySeq(<foo> 33 </foo>)
res4: Option[(String, String, scala.xml.MetaData, scala.xml.NamespaceBinding, Seq[scala.xml.Node])] =
Some((null,foo,,,ArrayBuffer(33)))
scala> scala.xml.Elem.unapplySeq(<foo> { x } </foo>)
res5: Option[(String, String, scala.xml.MetaData, scala.xml.NamespaceBinding, Seq[scala.xml.Node])] =
Some((null,foo,,,ArrayBuffer(, 33, )))
注意,你要在ArrayBuffer三个要素,只有一个第一:您可以通过调用unapplySeq
看到这个(这是用于模式匹配)。所以模式不正确。
相关问题
- 1. Scala XML with&in CDATA
- 2. Texturebrush with separator pattern
- 3. Scala Number type pattern matching
- 4. setTimeout with The Revealing Module Pattern
- 5. regexpr with preg_match(),pattern generate error
- 6. Scala Future with Option()
- 7. Gnuplot fillstyle pattern with word function given error
- 8. git - trim whitespace
- 9. whitespace UNION bug
- 10. Escape html和whitespace?
- 11. grails stack with scala and scaml
- 12. scala Seq sortWith or sortBy with NaNs
- 13. 如何解析文本以分隔符开始whitespace-tilde-whitespace“〜”?
- 14. C#Parallel.Foreach with XML
- 15. xml with BeautifulSoup
- 16. xslTransform with xml-stylesheet
- 17. xml fault with dewplayer
- 18. XmlSerializer和嵌入式的WhiteSpace
- 19. RegEx Whitespace Vs. Eclipse的
- 20. Bash,curl,eval和whitespace
- 21. PyLint bad-whitespace配置
- 22. XML到Json with Camel
- 23. xml tree with php + header
- 24. IRequest/IResponse Pattern
- 25. .with Scala中的替代方法
- 26. Scala XML Library for 2.10+
- 27. 用scala解析xml
- 28. Scala将XML导入地图
- 29. git svn windows linux whitespace问题
- 30. Isotope.js - 元素之间的WhiteSpace
Righty,谢谢!了解Elem.unapplySeq也非常方便。 – 2012-01-03 13:00:58