[libxml-devel] << operator
Masashi Shimbo
shimbo at is.naist.jp
Fri Aug 22 00:23:14 EDT 2008
On Fri, Aug 22, 2008 at 6:10 AM, Sean Chittenden <sean at chittenden.org> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
>>> XML::Node.new('foo') << XML::Node.new('bar') << XML::Node.new('baz')
>>> Should produce:
>>> <foo><bar><baz/></bar></foo>
>>> XML::Node<<(arg) should return the argument being appended *ONLY IF* the
>>> argument is an XML::Node, else it should return the xmlNode that had its
>>> content modified (e.g. XML::Attributes and Strings).
>>> That should solve this problem.
>>
>> So in this case the first << returns the 'bar' node and the second <<
>> returns the baz node.
>>
>> Thus:
>>
>> node = XML::Node.new('foo') << XML::Node.new('bar') <<
>> XML::Node.new('baz')
>>
>> node is baz. Which is annoying, because most of the time I imagine you
>> want foo (at least I want foo). Thus, returning the appended argument is
>> great, until the very end, where you want the original node...
The above case by Charlie very much summarizes one point of my objection.
And my impression is that all the discussion so far ignores
how to make trees with two or more children (easily).
It is not clear to me (or am I just stupid?) how to build a tree with
two or more children when << returns a child instead of self,
without variable assignments.
It is easy when << returns self:
A << (B << C) \
<< (D << E)
This makes A with two children B and C each with a
If you want a path (tree without branches),
you can still do
A << (B << (C << D)))
Masashi Shimbo
More information about the libxml-devel
mailing list