From: joswig@... Date: 2007-03-09T07:00:08+09:00 Subject: Re: Paul Graham explains Ruby symbols On Mar 7, 6:17 pm, "Rick DeNatale" wrote: > On 3/6/07, Luciano Ramalho wrote: > > > Paul Graham offers this excellent explanation for the symbol type: > > > "Symbols are effectively pointers to strings stored in a hash table. > > So you can test equality by comparing a pointer, instead of comparing > > each character." [1] > > > Of course, he's talking about symbols inLisp, but what he says > > applies equally well to Ruby and Smalltalk. > > I find this a little too implementation-centric a description. > > The key aspect of symbols is that two symbols are identical if they are equal. > The fact that there may (or may not be) a hash table used to ensure > this is irelevant. > > And that description really misses the boat as far asLispis > concerned.Lispsymbols aren't strings, they are really names to which > three (separate) things can be bound, a value (which can be anyLisp > object), a function, and a property list. Actually the name is also > one of the slots in a symbol. Actually it's five in Common Lisp: Value, Function, Name, Property List, Package. > Note that inLispthe value of a Symbol is separate from it's name, > and two Symbols can have the same value, they just can't have the same > name. No, that's wrong. Two different symbols can have the same name in Common Lisp. CL-USER 30 > (eq '#:foo '#:foo) NIL CL-USER 31 > (symbolp '#:foo) T CL-USER 32 > (symbol-name '#:foo) "FOO" > > So inLispa symbol is more like an entry in the table of global names. No. In Lisp a symbol is a data structure with above five (virtual) slots. Symbols can exist without a 'table of names'. These 'table of names' are called packages. A symbol can belong to a package. You can then lookup the symbol via the package by its name. Again a symbol can exist without being interned in a package. > > Symbols in Ruby and Smalltalk are more alike than Symbols inLisp. > > Smalltalk and Ruby symbols have unique 'values' which are also their 'names'. > These are a bit keyword symbols in Lisp. All symbols that belong to the keyword package have themselves as the value. CL-USER 30 > (eq '#:foo '#:foo) NIL CL-USER 31 > (symbolp '#:foo) T CL-USER 32 > (symbol-name '#:foo) "FOO" CL-USER 33 > :foo :FOO CL-USER 34 > (symbol-package :foo) # CL-USER 35 > (eq :foo (symbol-value :foo)) T CL-USER 36 > (describe ':foo) :FOO is a SYMBOL NAME "FOO" VALUE :FOO FUNCTION # PLIST NIL PACKAGE # > Most Smalltalk implementations make Symbol a subclass of String, > although I'm pretty sure that we didn't require this when we wrote the > X3J20 ANSI Smalltalk spec (We didn't require much if any particular > inheritance hierarchy). Ruby does not treat Symbols as Strings, > although one can obtain an instance of either from an instance of the > other. > > Not long ago Matz experimented with the idea of making Symbol a > subclass of String in 1.9, but this appears to have been dropped in > more recent versions. > > -- > Rick DeNatale