David said:
In any case, I believe the two things in your example are actually
equivalent. My understanding is that Ruby always expands:
x += y
to
x = x + y
Ouch! That's what I get for posting code without trying it out first.
Even something that trivial tripped me up, and I should know better.
Austin said:
"a += i" is syntax magic for "a = a + i". Perhaps you're referring
to something like:
def app1(a, i); a << i; end
def app2(a, i); a += i; end
Right, that is closer to what I was trying to illustrate. As to whether
those two should be considered equivalent, I assert each person's answer
will depend on their expectations and their prior experience.
Austin said:
Except that Ruby's call model isn't that far from the way that Perl
works. Probably not far from Python's model, either.
Perl generally uses call-by-value, but it also supports
call-by-reference using the backslash operator:
sub inc {
$arg = shift @_;
$$arg += 1;
}
$x = 1;
inc(\$x);
print $x;
This prints "2". Here, "\$x" is a reference to $x, and "inc" assigns it
to $arg. $$arg dereferences the reference, allowing inc to modify $x
directly.
Like Ruby, Python doesn't seem to have any support for
call-by-reference, and Python coders have been through the exact same
discussion that we're having here (almost word-for-word):
http://mail.python.org/pipermail/python-list/2001-June/048603.html
Florian said:
I'm not experienced at Tcl. What do those built ins do?
Tcl's "upvar" is a generalized call-by-reference method. It allows a
function to bind a local variable to variable from the calling scope.
proc inc name {
upvar $name a
set a [expr $a + 1]
}
set x 1
inc x
puts $x
This also prints "2". In this case, "a" becomes an alias for "x" after
the call to upvar. Tcl's "uplevel" is very similar in concept to
Binding.of_caller, allowing execution of code in the caller's scope.
Tcl coders can use uplevel to create new flow-of-control structures.
I've seen one or two posts here that flat out denigrate
call-by-reference, or suggest that it is a workaround necessitated by
weak language design. This is a narrow view, IMHO. If we can define
methods that modify an object in place, then we are already halfway to
using call-by-reference semantics.
Oh well, this was probably more educational for me than for anybody
else, so thanks to everybody for your patience.