Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion [Ruby 1.9 - Feature #4801][Open] Shorthand Hash Syntax for Strings
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Piotr Szotkowski  
View profile  
 More options May 30 2011, 4:55 pm
From: Piotr Szotkowski <chast...@chastell.net>
Date: Tue, 31 May 2011 05:55:39 +0900
Local: Mon, May 30 2011 4:55 pm
Subject: [ruby-core:36596] Re: [Ruby 1.9 - Feature #4801][Open] Shorthand Hash Syntax for Strings

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 => 0, :foo => 1, 'foo' => 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’t, 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 ‘any object can be a key, just as any object can be
a value’ is the general case, and ‘I want my Strings and Symbols
to be treated the same when they’re similar, oh, and maybe with
the nil handled separately for convenience’ 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’t see why it should – as long as it still
responds to #hash and #eql?, it’s 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’s nothing preventing you from subclassing Hash to
create StringKeyHash, SymbolKeyHash or even MonoKeyHash
that would limit the keys’ class to the first one defined.

How would you treat subclasses? Let’s 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’s 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.

— Piotr Szotkowski
--
// sometimes I believe compiler ignores all my comments

  signature.asc
< 1K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.