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. Regex lookbehind with alterations and whitespace
- 4. Scala Number type pattern matching
- 5. Spring MockMvc redirectedUrl with pattern
- 6. regexpr with preg_match(),pattern generate error
- 7. SVG line with the multiline pattern
- 8. setTimeout with The Revealing Module Pattern
- 9. Scala Future with Option()
- 10. Gnuplot fillstyle pattern with word function given error
- 11. Django Switch language with i18n pattern and normal url_pattern
- 12. scala 2.10Mx with playframework 2
- 13. grails stack with scala and scaml
- 14. scala Seq sortWith or sortBy with NaNs
- 15. JiBX trim-whitespace ambiguiti
- 16. 刪除Tab-whitespace?
- 17. jquery scrolltofixed whitespace
- 18. git - trim whitespace
- 19. git apply:error trailing whitespace
- 20. Escape html和whitespace?
- 21. whitespace UNION bug
- 22. Flot linegraph whitespace
- 23. BackboneJS with XML ajax
- 24. xml with BeautifulSoup
- 25. xml fault with dewplayer
- 26. Libgdx with android xml?
- 27. C#Parallel.Foreach with XML
- 28. xslTransform with xml-stylesheet
- 29. 如何解析文本以分隔符開始whitespace-tilde-whitespace「〜」?
- 30. XmlSerializer和嵌入式的WhiteSpace
Righty,謝謝!瞭解Elem.unapplySeq也非常方便。 – 2012-01-03 13:00:58