joesb.coe9
6/30/2006 6:30:00 AM
Just Another Victim of the Ambient Morality wrote:
> I'm kind of embarrassed to ask this but is there a reason why Ruby
> doesn't have variables that refer to other variables? I don't think Python
> has them, either, so there must be some reason all these "new age" languages
> feel that they don't need them but I can't seem to figure out what that is.
> While I would actually be hard-pressed to explain why pointers are
> essential in C++, I certainly can't do too much programming in that language
> without them! So, there's some sort of difference but I can't put my
> finger on it.
> Can anyone explain this to me?
> Thank you...
You cannot do anything complicate with C++ unless you use pointer
because.
1. C++ doesn't have closure, plus You cannot define nested function in
C++.
2. C++ is static type.
Without closure or nested function, if you have a variable and you want
to set up callback to change the value of that variable you have two
choice between.
* If the variable is known outside current method scope, just define a
function to change it's value. (this includes, instance variable, just
substitue global function callback with method callback).
int x;
void change_x_callback(int newval){ x = newval; }
void test(){
pass_callback(&change_x_callback);
}
* If the variable is local function variable you have no other way to
change it than passing pointer of that int.
void test(){
int x = 0;
pass_x_to_change(&x);
}
In ruby the ruby design will change to using closure
def test
x = 0
invoke_this {|new_val| x = new_val}
end
----
On the other hand, object and closure are equivelant. So you could in
theory, wrap any variable you want in object. and pass in. But because
C++ is static typing, it becomes expensive to create a class just to
capture a variable to change.
I don't know much about Boost, but it seems that they enabled
functional programming very well, I assume less pointer is needed in
Boost. But unless Boost become standard library, most use of C++ will
still require use of pointer in place of closure.
The fact the ruby has no pointer will be issue only if all Ruby library
are designed with pointer in mind. But, because it is so easy to pass
object and closure, all Ruby library are made to utilize the power of
duck typing and block.