unbinding a Proc?

E

Eric Mahurin

This is mostly a curiosity thing...

Is there a way to unbind a Proc from the binding it was created
in? What I mean by this is taking a Proc and looking
up/calling everything it refers to outside of itself and
replacing them with what was found - objects. Assignments to
the outside and method calls that modify things on the outside
should be considered illegal.

The application for doing this would be to get rid of many
*eval(string) calls which I think are evil because you are
parsing/recompiling code at run-time - a severe limitation to
optimization. I think many of the times that you use
*eval(string) instead of *eval(proc) (or Proc#call), you do so
because you take the values of certain variables and put it in
the string (with #{...}). You want these to be constant
instead of refer to these variables in your context all the
time. This effectively "unbinds" those variables from their
context.

Another useful thing might be rebinding a Proc to a different
Binding. But, I can't think of any applications off-hand.




__________________________________
Discover Yahoo!
Find restaurants, movies, travel and more fun for the weekend. Check it out!
http://discover.yahoo.com/weekend.html
 
E

ES

Le 5/6/2005 said:
This is mostly a curiosity thing...

Is there a way to unbind a Proc from the binding it was created
in? What I mean by this is taking a Proc and looking
up/calling everything it refers to outside of itself and
replacing them with what was found - objects. Assignments to
the outside and method calls that modify things on the outside
should be considered illegal.

The application for doing this would be to get rid of many
*eval(string) calls which I think are evil because you are
parsing/recompiling code at run-time - a severe limitation to
optimization. I think many of the times that you use
*eval(string) instead of *eval(proc) (or Proc#call), you do so
because you take the values of certain variables and put it in
the string (with #{...}). You want these to be constant
instead of refer to these variables in your context all the
time. This effectively "unbinds" those variables from their
context.

Another useful thing might be rebinding a Proc to a different
Binding. But, I can't think of any applications off-hand.

Proc#rebind would be very useful but I am still waiting for
a full-blown first-order method conversion, too.

E
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: unbinding a Proc?"

|Proc#rebind would be very useful but I am still waiting for
|a full-blown first-order method conversion, too.

Can you explain what you want? I am not sure how Proc#rebind would
work, nor "a full-blown first-order method conversion".

matz.
 
E

Eric Mahurin

--- Yukihiro Matsumoto said:
In message "Re: unbinding a Proc?"
on Sun, 5 Jun 2005 23:14:58 +0900, ES

|Proc#rebind would be very useful but I am still waiting for
|a full-blown first-order method conversion, too.

Can you explain what you want? I am not sure how Proc#rebind
would
work, nor "a full-blown first-order method conversion".

matz.

If ES is thinking like me it would be called like this:

Proc#rebind(anotherBinding)

It would rebind all variable references and method calls into
the original binding to anotherBinding.

I think this would be more useful though:

Proc#unbind

It would replace all variable references and method calls into
the original binding with the current objects they return.
This would be great for changing most *eval(aString) to
*eval(aProc.unbind). I assume *eval(aProc) is much better than
*eval(aString) especially from a ruby optimization standpoint
(and ruby2c).






__________________________________
Discover Yahoo!
Use Yahoo! to plan a weekend, have fun online and more. Check it out!
http://discover.yahoo.com/
 
E

ES

Le 5/6/2005 said:
Hi,

In message "Re: unbinding a Proc?"

|Proc#rebind would be very useful but I am still waiting for
|a full-blown first-order method conversion, too.

Can you explain what you want? I am not sure how Proc#rebind would
work, nor "a full-blown first-order method conversion".

Rebinding: normal binding works this way (as you probably know:):

class A
attr_accessor :a
def foo b
@a =3D 'in A'
b.bar {puts self.a}
end
end

class B
attr_accessor :a
def bar(&p)
@a =3D 'in B'
p.call
end
end

a =3D A.new
b =3D B.new
a.foo b # =3D> 'in A'

Same applies for all other circumstances, as well, the block is
bound at the time of its creation. While this is typically quite
satisfactory, on occasion it would be useful to rebind the block
at another location -essentially, it would be the same as (from
our example) eval()ing the {puts self.a} again wherever the #rebind
is called.

"Full-blown first-order method conversion": conversion referring
to change from the current way of working methods.. this idea is
perhaps best described by a hypothetical syntactic sugarization:
currently, def methodname is a primitive (not a method call itself),
but it could actually be a method that takes a block (the method
body) and binds it at the call location (i.e. to the class it is
defined in).

At the basic level, everything would be based on blocks, methods
just being a specific use case (probably with some convenience
added). A method dictionary might be a good way to think about
it for methods.

I have dabbled in trying to implement this (and it should not be
too hard for more experienced developers, although it would be
a shift in paradigm); the main problem I have run to is to figure
out what, instead of 'def', would be the primitive operation. That
and lacking skill :)

The disadvantage I see in this type of method handling is that
it may interfere with the conceptual model if you consider that
methods (or 'behaviours') should be intrinsic and inseparable to
and from objects.
=09=09=09=09=09=09=09matz.

E
 
L

Logan Capaldo

Hi,
=20
In message "Re: unbinding a Proc?"
=20
|Proc#rebind would be very useful but I am still waiting for
|a full-blown first-order method conversion, too.
=20
Can you explain what you want? I am not sure how Proc#rebind would
work, nor "a full-blown first-order method conversion".
=20
matz.
=20
=20

I think what he wants is (optionally) dynamically scoped closures.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top