[Laszlo-dev] The one thing I really miss from Lisp is symbols

P T Withington ptw at openlaszlo.org
Fri Jan 5 08:55:06 PST 2007


On 2007-01-05, at 10:32 EST, Henry Minsky wrote:

> I was looking at a JSON (www.json.org) specification , and I  
> realized one
> thing I miss in
> programming languages these days are symbols. JSON is pretty close  
> to an
> s-expression except for
> the lack of symbols (except for the special cases of 'true',  
> 'false', and
> 'null').
>
> None of the popular  scripting languages
> seem to have symbols. You'd think wth the interest in JSON that  
> people might
> start wondering
> if there is some way to have built-in interned strings supported by  
> their
> language...

Well, 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 _ {
   static var allSymbols = {};

   function _(name) {
     if (name in _.allSymbols) return _.allSymbols[name];
     this._name = name;
     return _.allSymbols[name] = this;
   }

   function toString() {
     return this._name;
   }
}

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?



More information about the Laszlo-dev mailing list