Robert Klemme
3/22/2005 3:26:00 PM
"Berger, Daniel" <Daniel.Berger@qwest.com> schrieb im Newsbeitrag
news:8FE83020B9E1A248A182A9B0A7B76E7358B33F@itomae2km07.AD.QINTRA.COM...
> > -----Original Message-----
> > From: Joel VanderWerf [mailto:vjoel@PATH.Berkeley.EDU]
> > Sent: Monday, March 21, 2005 10:02 PM
> > To: ruby-talk ML
> > Subject: unifying hash and proc syntax
> >
> >
> > Hey, crazy idea. Why not use the same syntax for hashes and
> > arrays?
>
> If Ruby had a "List" superclass from which Array and Hash descended,
> instead of the Enumerable mixin strategy, this would have been easier I
> think. Presumably (and I'm totally guessing here) a list would have
> been anything between square brackets:
>
> [1,2,3,4].kind_of?(List) # true
> [1,2,3,4].kind_of?(Array) # true
> [1,2,3,4].kind_of?(Hash) # false
>
> [1 => 2, 3 => 4].kind_of?(List) # true
> [1 => 2, 3 => 4].kind_of?(Hash) # true
> [1 => 2, 3 => 4].kind_of?(Array) # false
>
> (I believe someone else proposed this syntax earlier.)
Enumerable does serve the purpose of defining a common abstract interface
already. Note also, that lists are usually ordered and their elements
don't change positions unless explicit told to. This is not true for a
Hash - at least not as it's implemented right now. Personally I dislike
changing Hash to add the overhead of remembering insertion order as this
is likely to slow down Hash typical operations.
The other problem with a common superclass / mixin of Array and Hash is,
that sometimes you want them treated similar (i.e. both have [] and []=
and can be accessed indexed; you can view an Array as a specialized Hash
that allows only Fixnum keys) and sometimes you don't. I still haven't
seen something that would consolidate this better than Enumerable does.
> This is more along the path that Perl took, where hashes and arrays are
> both lists. Then the {} would then be free, FREE, to be used for
> whatever nefarious purposes we could devise.
The sole fact that you mention perl here makes the whole proposal
suspicious to me. :-) I'd rather find a ruby solution than to look at
other languages.
> Not being a language designer, I have no idea if this is better or
> worse, if the List superclass is really necessary (i.e. could we still
> use a mixin strategy?), or if it would even be possible to implement in
> Ruby 2.0.
Personally I don't think we really need this change. Just my 0.02EUR...
Kind regards
robert