From: Ryan Pavlik Date: 2003-02-17T06:25:47+09:00 Subject: Re: General programming language question On Mon, 17 Feb 2003 02:35:05 +0900 "Chris Pine" wrote: > Is there a language where variables are first-class objects (in which > case I imagine assignment to be a normal function/method)? Common LISP, of course. :-) But then, everything in Lisp is first-class... including the syntax of the language itself. Of course, in Lisp, there aren't really "variables", but rather symbols that are bound to values. Symbols are somewhat like ruby's symbols. You set them like this: (setq foo '(a b c)) This binds the list (a b c) to the symbol foo. Actually, these days I believe you use "setf" instead of "setq", which has certain extra hooks, but the principle is the same. > It seems like variables and assignment usually "sit under" the > language. Is there a theoretical reason why it's impossible (or a > practical reason why it's a really bad idea) to do it any other way? Nope. In fact, you should take it a step further. Why should the rules of the language itself "sit under" everything? For instance, there's a good deal of magic in most languages. Let's use Ruby as an example: "abc" => String 1234 => Integer (or whatever) /abc/ => Regexp These are all "built in". Why can't you add your own types that have their own special syntax? For instance, if you add a dollars-and-cents type, it'd be nice to be able to express it like this: $12.50 In most languages, you can't add to the syntax. Lisp, Forth, and a few other languages let you. > What would be another way? I don't know... I was thinking of > something like "variables as one-element hashes"... but of course you > need a variable to refer to the array, so we gained nothing. Well, Smalltalk uses a Dictionary, I believe, to map variables to their values, and there's no reason you can't make a language do similar. With Ruby, you can get a Binding and evaluate things inside it. I'm rooting for the ability to do something like this: varstr = "myvarname" sym = varstr.intern # => :myvarname sym.bind(42) p myvarname # => 42 The only thing you can't currently do is the sym.bind() deal from Ruby. (Doing it on the C side is easy.) So basically you've got your first-class variables. > Has anyone seen this in any language? If you're looking for first-class anything, chances are Lisp already has it. In this case, I believe a number of other languages (including Ruby) at least come close. -- Ryan Pavlik "Why are we even *mentioning* semi-regular polyhedrons and real space?" - 8BT