Speaking of Lisp...

P T Withington ptw at pobox.com
Fri Jan 5 15:48:19 PST 2007


One of our developers wondered why no scripting language has adopted  
symbols.  I replied:

> Objects have most of the features of symbols:  they are unique, you  
> can compare them for identity, you can store properties on them.   
> An object by itself is like an uninterned symbol -- you have to  
> keep a handle on it.  If you want to intern an object, you can just  
> assign it to a global variable.  So, I think what you are mostly  
> missing is the shorthand where in lisp you can write
> 
>   'foo
>
> and if the symbol already exists, you get the symbol, and if it  
> doesn't it will be created for you.
>
> It wouldn't take much to mimic that in Javascript.  Something like:
>
> class Symbol {
>   static var allSymbols = {};
>
>   function Symbol(name) {
>     if (name in Symbol.allSymbols) return Symbol.allSymbols[name];
>     this._name = name;
>     return Symbol.allSymbols[name] = this;
>   }
>
>   function toString() {
>     return this._name;
>   }
> }
>
> var _ = Symbol;
>
> Then you can write a symbol as:
>
>   _('mySymbol')
>
> Clearly, IWBNI there were a shorter hand for writing symbol  
> literals, 1 character rather than 5, but this isn't too bad is it?

Were symbols ever discussed as a possible addition?  Is there a handy  
non-symbol punctuation mark that could be co-opted?

   @mySymbol

Dylan uses:

   #"mySymbol"

(3 characters rather than 5).  Yet another use for `#`.






More information about the Es4-discuss mailing list