[REBOL] Re: Pointers, Context in Rebol 3.0 (For the real Rebol Lovers)
From: Izkata:Comcast at: 23-Mar-2007 23:06
On Fri, 2007-03-23 at 21:17 +0100, Giuseppe Chillemi wrote:
> Now lets execute the following:
>
> a1: func [a b] [a / b]
>
> inst1: make template [f1: :a1]
> inst2: make template [f1: :a1]
>
> I expect that both inst1 and inst2 share the same "pointer to a1"
>
> Performing a:
>
> a1: func [a b] [a / b + 1]
>
> should change both INST1/f1 and INST2/f1 because they should point to the
> same function as I have declared [F1: :A1]. Instead only a1 is changed
> because a1 is copied during object creation and not pointed.
> For rebol 3.0 I ask you the ability to point to an external function like
> having a pointer to a function in C. Changing the pointed function should
> change the working of F1 in both the declared objects.
a1 is already a pointer to a function, not the function itself. That's
why INST1/f1 and INST2/f1 aren't changing - you're making a new function
and pointing a1 at it rather than the one INST1/f1 and INST2/f1 are
pointing at.
Take a look at this:
>> T: make object! [
[ f1: none
[ aaa: 1
[ ]
>> A: func [a b][a + b + aaa]
>> I1: make T [f1: :A]
>> print mold get in I1 'f1
func [a b][a + b + aaa]
>> print mold get 'A
func [a b][a + b + aaa]
>> remove back tail second get 'A
== []
>> append second get 'A 1
== [a + b + 1]
>> print mold get in I1 'f1
func [a b][a + b + 1]
f1: :A does not create a pointer to 'A, if pointers are how you want to
think of it. It gets what 'A is pointing at, then points at the same
thing. Setting a new value for 'A makes it point at something else -
modifying it as above without making it point at something else will
modify an others that are also pointing at it.
The only downside is that the function arguments don't seem to be
changeable:
>> clear second get 'A
== []
>> append second get 'A [print [{The answer is: } a * 2]]
== [print ["The answer is: " a * 2]]
>> clear first get 'A
== []
>> append first get 'A [a]
== [a b a]
>> print mold get in I1 'f1
func [a b][print ["The answer is: " a * 2]]
>> print mold get 'A
func [a b][print ["The answer is: " a * 2]]
>> clear first get 'A
== []
>> print mold get 'A
func [a b][print ["The answer is: " a * 2]]
(These results are the same in both View 1.3.2/Core 2.6.3 and Core 2.7.5
on Ubuntu Linux.)