Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Everything Is References

Name: Anonymous 2010-08-20 16:29

Is there a language in which variables, function arguments, etc, are always references by default?

For example:

int x = 5;
int y = x;
y++;  // x and y are both 6


If you really did want a copy of something, you'd have to use a keyword:

int x = 5;
int y = copy(x);


And pass-by-value would also have to be done explicitly:

void func(int z)
{
   int w = copy(z); // now w is effectively "local"
}


It would force programmers to at least be aware of the fact that they're making a copy.

Name: Anonymous 2010-08-20 17:31

>>1
All Lisps are like this. Lots of other languages too, like Python and Smalltalk.

You don't notice it in Python (and friends) because many objects that look like value types are actually immutable references. So when you pass an integer to a function and it does "foo += 5", that's actually translated to "foo = foo + 5". This code means a new integer object is created which is the sum of foo and 5, and then the assignment changes the foo reference to point to the new object. This way it doesn't change the integer object you called it with.

In Lisps though (well except Clojure), there are no immutable objects. If someone passes you an int, you can set! or setf on it, and they get the change as well (since it's the same object). This makes it very flexible and powerful, but also difficult to compile efficiently. You need whole-program analysis to verify that a tree of functions will not modify your int in order to pass it on the stack or in a machine register; if the compiler can't prove that it won't be modified, then it needs to heap-allocate it and pass in a pointer (leaving it to be garbage collected later.)

>>5
No, your idea was already done in 1958 in the first version of Lisp. Don't feel bad though; it's a really good idea.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List