[REBOL] word Re:(2)
From: fantam:mailandnews at: 14-Aug-2000 13:22
What a wonderful explanation. Thank you for the insight, Larry.
> HI Fantam
> The problem is that the word 'member inside the block referenced by 'code is
> bound to the global context when the script is loaded, and it remains so
> when the "do code" is executed inside the foreach loop. However,
> foreach member block [...]
> creates a local variable, also called 'member which will be set (bound)
> successively to the items contained in 'block. The name of the iterator
> variable is irrelevant to the problem. You will get the same problem if you
> say
> foreach x block [...]
> because the local variable 'member is never used in the foreach body block.
> You can use the function 'bind to bind the words in the external (to the
> function foreach) 'code block to the local context of the foreach function
> by binding the block to the local variable 'member.
>>> block: ["a" "b" "c"]
> == ["a" "b" "c"]
>>> code: [print member] ;context for member made here, value given later
> == [print member]
>>> member: 5
> == 5
>>> foreach member block [do code]
> 5
> 5 ;member in code bound to global context
> 5
>>> foreach member block [do bind code 'member]
> a
> b ;member in code bound to local var member
> c
> The latter gives the same result as moving the 'code definition inside the
> foreach loop.
>>> foreach member block [code: [print member] do code]
> a
> b
> c
> Some caution is necessary when using advanced functions like bind. For
> instance, in a more general case than yours, there might be some words in
> the external block that need to retain the values of their global binding
> despite the fact that those words are also used locally in the calling
> function and others that need to be rebound to the calling function's local
> context. In such a case, using bind as above will not give the desired
> effect.
> The thing to remember is that words have values only within a context, and
> the same word may have different values in different contexts.
> HTH
> -Larry
> ----- Original Message -----
> From: <[Fantam--mailandnews--com]>
> To: <[list--rebol--com]>
> Sent: Sunday, August 13, 2000 8:39 AM
> Subject: [REBOL] word
>> I am confused. Consider this small script:
>>
>> REBOL []
>>
>> block: ["a" "b" "c"]
>> code: [print member]
>> foreach member block [do code]
>>
>> Upon execution, I get :
>>
>> ** Script Error: member has no value.
>> ** Where: print member
>> >>
>>
>> I guess I have to use 'bind or 'use somewhere, but I'm not sure which
>> and how.
>>
>> Thanks for your help
>> --
>> Fantam
>>
>>
--
Fantam