lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

2010/1/20 Sam Roberts <>:
> I'm baffled as to what languages you have used before that make this surprising!
> Its like this in lua, python, perl, ruby, bash, c++, java, even
> fortran and lisp...
> Is there a language in which after
>  p = q + z
> p is actually q, but q was modified? wow. doesn't that mean that
> addition is not commutative?

sorry, may be I was not clear. I mean, the statements:
p = q
p = q + z
are quite similar from a mathematical point of view because both
assign a value to p that depends on the value of q.

The problem is that in lua they are subtly different because in the
first statement p points actually to the same object of q while in the
second case p point to a different objects and any changes in p will
not affect q. To have something logical as in mathematics the
p = q
should produce a new object for p that it is independent from q. This
is not done in Lua or Python because it would be expensive and will
lead to the creation of a myriad of small objects but the drawbacks is
that the programmer have to pay a lot of more attentions to be sure
that side effects are not added unintentionally.

The other approach is the ML approach where the object are immutable
once that have been assigned at the beginning. In this way you don't
care if an object is the same or not because in any case it cannot be
changed and you cannot have side effects. This is very much the same
of what you achieve in Lua with string interning.