context question
[1/4] from: kpeters::otaksoft::com at: 31-Aug-2007 15:21
Why does Rebol not complain about 'f2text not having a value in 'show-form2???? It forces
me to use the context to talk to the layout and the 'change-text function which makes
good sense to me. How come it "sees" the "sub-part" 'f2text when it cannot "see" its
parent? Would one of the gurus please shed some light on this for me? As always, thanks
a lot! Kai rebol[] ;------------------------------------------- show-form2: does [ unview
view/new ctx/form2 ; makes good sense ctx/change-text ; makes good sense set-face f2text
"GREEN" ; ?????? ] ;------------------------------------------- form1: center-face layout
[ btn "Show form 2" [ show-form2 ] ] ;------------------------------------------- ctx:
context [ change-text: does [ set-face f2text "BLUE" ] ; form2: center-face layout [
f2text: text "RED" ] ] ;------------------------------------------- view form1 ;----------------
E o S --------------------
[2/4] from: anton::wilddsl::net::au at: 1-Sep-2007 22:16
Hi Kai,
A deeper understanding of CONTEXT is required.
CONTEXT just MAKEs an object!
When MAKEing an object, the spec block is first scanned
for set-words, but only in the top level.
Those set-words that were found in the scan are added
to the new object. eg:
probe context [a: none do [b: none]]
Only A is added to the new object because B: is in a sub-block.
In your example, when building CTX, only CHANGE-TEXT: and
FORM2: were found in the top level, which you can verify:
probe first ctx
If you want a word added to a context which is only
set in a sub-block, then set it to none in the top-level first.
eg:
context [
f2text: none
change-text: ...
...
]
And that explains why F2TEXT was available to SHOW-FORM2;
F2TEXT was not bound to CTX, so when you set it, it was
set global.
Anton.
[3/4] from: kpeters::otaksoft::com at: 1-Sep-2007 8:34
Thanks again, Anton ~
for taking the time to explain. Just what I needed - that
part is now understood.
Wouldn't it be neat if context had a /deep refinement?
Thanks,
Kai
On Sat, 1 Sep 2007 22:16:40 +1000, Anton Rolls wrote:
[4/4] from: anton::wilddsl::net::au at: 3-Sep-2007 22:06
No worries.
Context /deep;
That sort of thing is coming in Rebol 3 (with modules).
But it's possible to make your own function
which searches deeply for set-words when
constructing a new object. eg:
context-deep: func [spec][
; 1 search spec block recursively for set-words and collect them
; 2 create new object containing each found set-word
; 3 DO a copy of the spec, bound to the new object.
; 4 return the object
]
Something similar to that.
I have probably forgotten if Ladislav or Gabriele has
already done something like this.
Check Ladislav's site.
Anton.