From: Piotr Szotkowski Date: 2011-05-31T05:55:39+09:00 Subject: [ruby-core:36596] Re: [Ruby 1.9 - Feature #4801][Open] Shorthand Hash Syntax for Strings --+pHx0qQiF2pBVqBT Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Cezary: > I though exactly the same thing, until I realized > that having keys of different types in a Hash > isn't really part of the general Hash concept. Why? [citation needed] > Consider the following: > { nil =3D> 0, :foo =3D> 1, 'foo' =3D> 2 } > Conceptually, people expect Hash keys to be of the same type, > except maybe for "hacks" like that nil above that can simplify code. Well, they either do or don=E2=80=99t, then. :) > If someone out there in the world actually demands that such a Hash > is valid and that :foo and 'foo' are different keys, you could always > wrap Hash to support that for that single, specialized case. Hm, IMHO =E2=80=98any object can be a key, just as any object can be a value=E2=80=99 is the general case, and =E2=80=98I want my Strings and Sy= mbols to be treated the same when they=E2=80=99re similar, oh, and maybe with the nil handled separately for convenience=E2=80=99 is the specialised case. > In Ruby "foo" + 123 raises a TypeError. Adding a string > key to a symbol-keyed Hash doesn't even show a warning. I don=E2=80=99t see why it should =E2=80=93 as long as it still responds to #hash and #eql?, it=E2=80=99s a valid Hash key. Hashes in Ruby serve a lot of purposes (they even maintain insertion order); if you want to limit their functionality, feel free to subclass. > I consider hashes with different key types different types of hashes, > that shouldn't even be allowed to merge together without conversion. There=E2=80=99s nothing preventing you from subclassing Hash to create StringKeyHash, SymbolKeyHash or even MonoKeyHash that would limit the keys=E2=80=99 class to the first one defined. How would you treat subclasses? Let=E2=80=99s say I have a Hash with keys being instances of People, Employees and Volunteers (with Employees ans Volunteers being subclasses of People). Should they all be allowed as keys in a single MonoKeyHash or not? What about String-only keys, but with different keys having their own different singleton methods? (For discussion=E2=80=99s sake: what about if a couple of the Strings had redefined #hash and #eql? methods, on an instance level?) > I think the meaning of symbols and hashes are too similar for such > different types to be allowed as keys in the same Hash instance. But that would introduce a huge exception in the current very simple model. Ruby is complicated enough; IMHO we should strive to make it less complicated, not more. =E2=80=94 Piotr Szotkowski --=20 // sometimes I believe compiler ignores all my comments --+pHx0qQiF2pBVqBT Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iEYEARECAAYFAk3kA/UACgkQi/mCfdEo8UoOswCgzz9tm+z3ARnybr09PH9vTQR1 4ucAoLCrW92gFCSB5HxL0i+ntGpLY4rC =iClu -----END PGP SIGNATURE----- --+pHx0qQiF2pBVqBT--