[REBOL] Re: local vars in functions
From: rotenca:telvia:it at: 1-May-2002 19:33
Hi Ladislav,
> No, I think, that my code works. (Test it, please, and let me know if
> anything doesn't work as it should).
...
> Why do you suggest this form? (I never wrote code looking like that - loop 1
> or catch are unusual for me in this case)
For test, try the code at the end. The new version use the catch loop trick
(the make object! spec solution was a joke: "definition must not contain the
word defined", ipse dixit). In the previous message i tryed to explain the
problem: any throw, return, exit, break is catched by make object!, your
simulation, instead, execute them like normal instrutions.
-------------
rebol []
;a new version of blank-object
blank-object: func [
{make a blank object}
blk [block!]
] [
unset in blk: context compose [return self (blk)] 'self
blk
]
;original
sim-make-object: function [
{MAKE OBJECT! simulation}
[throw]
spec [block!]
] [set-words object sw] [
; find all set-words in SPEC
set-words: copy []
parse spec [
any [
copy sw set-word! (append set-words sw) |
skip
]
]
; create a blank object
object: blank-object set-words
; set 'self in object to refer to the object
object/self: object
; bind the SPEC to the blank object
spec: bind spec in object 'self
do spec
; return the value of 'self as the result
get/any in object 'self
]
;the patched sim-make
new-sim-make-object: function [
{MAKE OBJECT! simulation}
[throw]
spec [block!]
] [blk object sw] [
blk: copy [exit]
parse spec [
any [
copy sw set-word! (insert tail blk sw) |
skip
]
]
object: make object! blk
catch [loop 1 [do does bind spec in object 'self]]
get/any in object 'self
]
;the text code
foreach blk [ [a: 1 exit] [b: 2 return] [c: 3 break] [d: 4 throw 1]] [
print ["--------" mold blk "---------"]
ori: new: old: none
ori: make object! copy blk
new: new-sim-make-object copy blk
loop 1 [
catch [
do does [
old: sim-make-object copy blk
]
]
]
?? ori
?? new
?? old
]
halt
---
Ciao
Romano