Accessing a variable's method
[1/5] from: varnoux::atmel::fr at: 10-Oct-2003 17:35
Hi List,
I have a list of objects:
/l: copy []
obj: make object! [ a: none calc: does [self/a: (self/a + 1)]]
append l make obj [a: 1]
append l make obj [a: 2]/
I would like to write a function like:
/inc-obj: func [arg][
arg/calc
]/
That would allow me to do:
/foreach o l [
inc-obj o
]
/
And output:
/probe (first l)/a/
/2
//probe (second l)/a/
/3
/Is it possible ?
Best regards,
Vincent
[2/5] from: varnoux:atmel at: 10-Oct-2003 17:46
A few anti-slashes have been added (dunno know why...) :
Hi List,
I have a list of objects:
l: copy []
obj: make object! [ a: none calc: does [self/a: (self/a + 1)]]
append l make obj [a: 1]
append l make obj [a: 2]
I would like to write a function like:
inc-obj: func [arg][
arg/calc
]
That would allow me to do:
foreach o l [
inc-obj o
]
And output:
probe (first l)/a
2
probe (second l)/a
3
Is it possible ?
Best regards,
Vincent
[3/5] from: joel:neely:fedex at: 10-Oct-2003 10:58
Hi, Arnoux
It's simple (simpler than that, actually ;-)
Arnoux Vincent wrote:
> Hi List,
> I have a list of objects:
<<quoted lines omitted: 17>>
> /3
> /Is it possible ?
Yes, and you don't need the overhead of INC-OBJ or the repeated uses
of SELF within the method on your objects. See transcript below:
>> proto: make object! [
[ count: 0
[ bump: func [] [count: count + 1]
[ ]
>> obj-block: []
== []
>> repeat i 4 [append obj-block make proto [count: i]]
== [
make object! [
count: 1
bump: func [][count: count + 1]
]
make object! [
count: 2
...
>> foreach obj obj-block [obj/bump]
== 5
>> foreach obj obj-block [print obj/count]
2
3
4
5
>>
--
----------------------------------------------------------------------
Joel Neely joelDOTneelyATfedexDOTcom 901-263-4446
Counting lines of code is to software development as
counting bricks is to urban development.
[4/5] from: ingo:2b1 at: 10-Oct-2003 18:04
Hi Arnoux,
Arnoux Vincent wrote:
> A few anti-slashes have been added (dunno know why...) :
> Hi List,
<<quoted lines omitted: 11>>
> inc-obj o
> ]
Up to here, no problem. (though I don't know why you don't want to use
foreach o l [ o/calc ]
> And output:
> probe (first l)/a
> 2
> probe (second l)/a
> 3
But this won't work, because parens!s are not allowed in paths ... however
you can do any of the following ...
probe l/1/a
== 2
probe l/2/a
== 3
num: 1
probe l/:num/a
== 2
and becoming somewhat esoteric ...
probe get in first l 'a
== 2
I hope that's enough for now ...
Ingo
[5/5] from: varnoux:atmel at: 10-Oct-2003 20:15
Thanks, Joel, this worked like a charm.
I was somehow doubty about passing a pointer as an rgument to a rebol
function... ;-)
Ingo > Thanks to you also. I wanted to use the function because this was
the structure (a little bit more complex) of my program.
Vincent
Joel Neely a écrit :
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted