Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Binding a changed function within a context

From: gscottjones:mchsi at: 5-Oct-2002 7:27

Hi, List, I am working on an enhanced version of a field widget. In order to get the enhanced functionality, I need to modify a function within the context of edit-text in REBOL. The code is too long for easy use on the list (in my opinion), so I have created a very simple example that (hopefully) recreates the problem in the "small." Here is my hypothetical context. It contains a local variable and a function that prints that variable. my-context: context [ local-var: 5 my-func: does [ print local-var ] ] where then executing: my-context/my-func ;yields 5 Now, let us say that I wish to dynamically change the function in this object to the following: my-func: does [ print ["new print" local-var] ] where my-func will be bound into the context of my-context. For your ease in running the code samples, I have included the original context object with each example. In my non-hypothetical case, the shortest code method would be to change the code in place like: ;### Method 1 ;original my-context: context [ local-var: 5 my-func: does [ print local-var ] ] ;make change insert remove remove pick pick pick my-context 2 3 2 [print ["new print" local-var]] This makes my-context "look" correct (meaning that the data representation of the code is correct when viewed with probe), but the inner function is not bound into the context: my-context/my-func ;expectedly yields an error For completeness (to be sure that I understood the precept behind bind), I also directly reassigned the function: ;### Method 2 ;original my-context: context [ local-var: 5 my-func: does [ print local-var ] ] ;make change my-context/my-func: does [ print ["new print" local-var] ] and again confirmed that an error occurs with my-context/my-func, because it has not been bound into the context. In the past, my tried and true way to make it work has been to "re-make" the object with the new function: ;### Method 3 ;original my-context: context [ local-var: 5 my-func: does [ print local-var ] ] ;make change my-context: make my-context [ my-func: does [ print ["new print" local-var] ] ] which then works correctly when executing my-context/my-func (yields "new print 5"). I understand the basic concepts behind scope and binding, and how these play out in REBOL. It seems as though it should be easy enough to use bind, but I can't figure out how to (successfully) do this when changing a function within a context, at least with methods 1 and 2. Nothing wrong with method 3, but I would love to also be able to use method 1 or 2 with a strategically and correctly placed bind statement. Any ideas? --Scott Jones