这是 scala.xml.Elem 中的错误吗?
将 Set[String] 映射到 Set[Elem] 的属性对于一个属性可以正常工作,但当共享其中一个属性时,对于多个属性似乎会失败:
scala> val s1=Set("A","B","C")
s1: scala.collection.immutable.Set[java.lang.String] = Set(A, B, C)
scala> s1.map((a:String)=>{<X w={a}></X>})
res3: scala.collection.immutable.Set[scala.xml.Elem] = Set(<X w="A"></X>, <X w="B"></X>, <X w="C"></X>)
scala> s1.map((a:String)=>{<X w={a} k="SSS"></X>})
res4: scala.collection.immutable.Set[scala.xml.Elem] = Set(<X k="SSS" w="A"></X>)
B & 发生了什么? C?
或者我对 Elems 上的 equals 的理解是错误的?
Mapping a Set[String] to attributes of a Set[Elem] works fine with one attribute, but seems to fail with multiple attributes when one of them is shared:
scala> val s1=Set("A","B","C")
s1: scala.collection.immutable.Set[java.lang.String] = Set(A, B, C)
scala> s1.map((a:String)=>{<X w={a}></X>})
res3: scala.collection.immutable.Set[scala.xml.Elem] = Set(<X w="A"></X>, <X w="B"></X>, <X w="C"></X>)
scala> s1.map((a:String)=>{<X w={a} k="SSS"></X>})
res4: scala.collection.immutable.Set[scala.xml.Elem] = Set(<X k="SSS" w="A"></X>)
What happened to B & C?
Or is my understanding of equals on Elems wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题出在
xml.MetaData
或其子类xml.Attribute
中。比较两个Elem
时,会检查 xml 前缀、标签、属性和Node
子级是否相等。现在,
xml.MetaData
在下面有一种奇怪的实现,它包含自身,同时包含属性的链接列表。这意味着,例如:例如,它生成一个自身列表,并删除了 head 属性。
MetaData
中的相等性检查看起来像这样,工作正常,但在
Attribute
类中被覆盖。 (我们在 REPL 中看不到它,但我们的elem.attributes
确实是一个Attribute
。)在那里,代码只执行此操作,如果属性列表之前已转换为
Set
,但事实并非如此,因此仅检查属性列表中的第一个元素是否相等。因此,如果两个xml.Elem
的内部属性链表中的 head 元素恰好是相同的元素,则它们将相等。I think the problem lies in
xml.MetaData
or its subclassxml.Attribute
. When comparing twoElem
s, it is checked for equality of the xml prefix, the label, the attributes and theNode
’s children.Now,
xml.MetaData
has kind of a strange implementation underneath, it contains itself and at the same time a linked list of attributes. That means that, for example:E.g. it generates a list of itself with the head attribute removed.
The equality check in
MetaData
looks like thiswhich works okay but is overridden in the
Attribute
class. (We can’t see it in the REPL, but ourelem.attributes
really is anAttribute
.) There, the code only does thisWhich would be fine, if the attributes list were converted to a
Set
before but it isn’t and so only the first element in the attributes list is checked for equality. And thus, if the head element in the internal linked list of attributes happens to be the same element for twoxml.Elem
s, they will be equal.这确实是一个错误,尽管是一个非常奇怪的错误。我猜这是
Set
中的错误,而不是Elem
中的错误,但在这一点上我无法与任何权威人士交谈。This does appear to be a bug, albeit a very strange one. I would guess it's a bug in
Set
and notElem
, but I can't speak with any authority on that point.