So if you can "append" to a set by
adding a value, why not append to a hash by adding a pair?
Okay, fair enough.
However, I will point
out that the semantics of the proposed Hash#<< and Set#<< *are*
different. Array#<< and Set#<< each expect a single value; if you give
another Array or Set as the argument, it's added as a unit, rather
than iterating over the contents.
I think this is closer to the point I was trying (unsuccessfully) to make. I
think that having Hash#<< is useful only so long as it complies with the
semantics of what << normally does. As proposed, Hash#<< is really just
Hash#merge! in diguise. There is no reason to make a semantically ambiguous
definition (which you pointed out above) when you could simply use #merge!
instead.
You could write Hash#<< to only accept a single key/value pair and be
semantically meaningful, but there are a few problems with this:
1) As pointed out,
{ :foo => bar } << :a => 1
still doesn't work. It has to be
{ :foo => bar }.<< :a => 1
which IMO is just ugly.
2) When someone tries to append a Hash to a Hash with #<< , what happens?
{ :foo => bar }.<< { :a => 1, :b => 2} #ERR
You can't really do this because it still doesn't make semantic sense. The
whole hash should get added (not each element), but without a key, you can't
add it.
3) Because of the way a Hash works, you can't add multiple values for the
same key, and this is how << is supposed to work. When you say
[1,2,3,4] << 4
=> [1,2,3,4,4] #not [1,2,3,4]
The behavior here is what you would expect from something that "appends,"
while Hash#<< doesn't do the same.
I'm not saying it's a terrible, evil, awful thing to have Hash#<<. I just
think you should think there are a lot of potential negatives, especially
when you could just as easily say Hash#merge!.
···
On 6/29/06, Jacob Fugal <lukfugl@gmail.com> wrote:
Dean