Executable code blocks as arguments
[1/5] from: tim::johnsons-web::com at: 22-Aug-2001 9:56
Hello All:
Is it possible to pass a code block to a function member of an object
which would perform adhoc operations on object members?
Below is an example which I have set up:
REBOL []
o: make object![
a: 1 b: 2 c: 3
modify: func[arg[block!]][do arg]
]
print mold o
o/modify [a: "one" b: "two" c: "three"]
print mold o
; now the output below
make object! [
a: 1
b: 2
c: 3
modify: func [arg [block!]][do arg]
]
make object! [
a: 1
b: 2
c: 3
modify: func [arg [block!]][do arg]
]
; as you can see, 'a, 'b, and 'c remain unmodified.
; The result that I would be looking for would be:
make object! [
a: "one"
b: "two"
c: "three"
modify: func [arg [block!]][do arg]
]
Can I do this at all? If so, thanks in advance for
advice and comments, and feel free to point me to
docs on this.
regards
Tim
[2/5] from: ryanc:iesco-dms at: 22-Aug-2001 10:27
The context of the block arg must be bound to the current context.
>> ob: make object! [
[ a: 1
[ f: func [v] [bind v 'a do v]
[ ]
>> ob/a
== 1
>> ob/f [a: 5]
== 5
>> ob/a
== 5
>>
have fun, --Ryan
Tim Johnson wrote:
> Hello All:
> Is it possible to pass a code block to a function member of an object
<<quoted lines omitted: 38>>
> [rebol-request--rebol--com] with "unsubscribe" in the
> subject, without the quotes.
--
Ryan Cole
Programmer Analyst
www.iesco-dms.com
707-468-5400
[3/5] from: cyphre:volny:cz at: 22-Aug-2001 19:06
Hello Tim,
try this:
o: make object![
a: 1 b: 2 c: 3
modify: func[arg[block!]][do bind arg 'a]
]
the magic is in 'bind ;-)
USAGE:
BIND words known-word /copy
DESCRIPTION:
Binds words to a specified context.
BIND is a native value.
ARGUMENTS:
words -- A block of words or single word. (Type: block word)
known-word -- A sample word from the target context. (Type: word)
REFINEMENTS:
/copy -- Deep copies block before binding it.
Have fun!
Cyphre
[4/5] from: larry:ecotope at: 22-Aug-2001 10:46
Hi Tim
The code block you pass in has to be bound to the objects context. Change
your modify function like this:
REBOL []
o: make object![
a: 1 b: 2 c: 3
modify: func[arg[block!]][do bind arg 'self]
]
>> o/modify [a: "one" b: "two" c: "three"]
== "three"
>> probe o
make object! [
a: "one"
b: "two"
c: "three"
modify: func [arg [block!]][do bind arg 'self]
]
HTH
-Larry
[5/5] from: tim:johnsons-web at: 22-Aug-2001 11:36
On Wed, Aug 22, 2001 at 10:27:14AM -0700, Ryan Cole wrote:
> The context of the block arg must be bound to the current context.
> >> ob: make object! [
<<quoted lines omitted: 8>>
> == 5
> >>
Yes!! That's it.....
> have fun, --Ryan
Oh boy! Will I ever!
Thanks :>)
tj
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted