[REBOL] Re: Accessing a variable's method
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:
> /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 ?
>
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.