Symbols and Strings

I’ve been thinking about Symbols today.

In Ruby 1.4, they were basically fancy Fixnums. They were immutable and immediate,
and their values didn’t really matter. (Back then, as I recall, you could actually do a puts
of a symbol and get a numeric result. I don’t recall that there was a Symbol class, though
there may have been.)

Symbols are still immutable and immediate (though we don’t mentally associate them with
integers any more). Their semantics has changed little, except that in recent versions of
Ruby I believe they are stored on the heap and thus can be garbage-collected (to avoid an
exploit where an app might create an arbitrary number of symbols and force an out-of-memory
error).

But it occurs to me now that much of their use is stringlike. I do find myself converting back and
forth between symbols and strings fairly often (especially to strings — not so often the reverse).

Consider also mental “nearness” of strings and symbols. This has led many people to use the
Rails function with_indifferent_access — a practice I won’t support or decry here.

So I have a germ of an idea. A knowledgeable person may be able to shoot it down quickly –
the likes of Matz himself, Dave Thomas, and a dozen others whom I consider giants. Others,
despite lacking demigod status, may have useful points to make, or may have strong
opinions.  ;)

The idea is simply: Let Symbols be nothing more or less than immutable Strings. In fact, let
Symbol inherit from String.

No more with_indifferent_access. No excessive to_s and to_sym scattered everywhere. No
more asking: Does this return a Symbol or a String?

What do you think?

About these ads

1 Comment

  1. Ha… someone else had this idea years ago. It was implemented and later rejected.

    Silly me…

    “Those who cannot remember history are condemned to Google it.”


Comments RSS TrackBack Identifier URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.