Hello [EMAIL PROTECTED],
Is something like this what you had in mind ?
REBOL [
Title: "Globalizer"
Author: "Thomas Jensen"
email: [EMAIL PROTECTED]
File: %globalizer.r
Date: 15-Jun-2000
]
; make sure we're not fooled by previous runs :-)
unset [local-var x y test-scope globalize]
; make a scope or context (ie. object!)
test-scope: make object! [
local-var: 3
x: func [num] [num + local-var]
y: func [num] [num - local-var]
]
; the funny function
globalize: func [
{Export the words in an object! to the global context, while keeping their binding}
o [object!] "Object to globalize"
/except "Exclude some words from beeing exported"
except-words [block!] "Don't globalize these words"
] [
if none? except-words [except-words: []]
foreach word (next first o) [
if not found? find except-words word [
set (in system/words word) (get in o word)
]
]
]
; now "globalize" the test-scope object, except 'local-var
globalize/except test-scope [local-var]
; 'x and 'y are now globally visible
print x 5 ; should print 8
print y 5 ; should print 2
; now change the value of 'local-var in the test context
test-scope/local-var: 5
print x 5 ; should print 10
print y 5 ; should print 0
print value ; should generate and error
; --- end of script ---
Best regards
Thomas Jensen
On 15-Jun-00, [EMAIL PROTECTED] wrote:
>
>
>
> I have some routines where it references the same
> string over and over and over.
>
> e.g.
>
> somefunc: func [
> someparam [string!]
> /local
> result ; [string!]
> ][
> x result
> y result
> z result
> append result "Blah"
> { etc. ad nauseum }
>
> return result
>
> ]
>
> And I noticed that I was using "result" as the first parameter
> passed to nearly everything.
>
> Well, your first thought is that maybe you ought to make
> result just be a global variable. But not only does it
> go against one's training and experience, but
> in fact I actually have a case where somefunc calls somefunc2,
> a routine that does something similar, and that would mess
> me up.
>
> So, how do I have a sort of almost global variable in the sense that
> I can set it and then have all these functions know about it without
> having to pass the darn value all around to everyone of them?
> It gets tedious and one hopes that an advanced language
> like Rebol might have some tricks to cure this ailment.
>
> It reminds me a little bit of execution-scope? where the scope
> is defined by the block where it is first defined (or redefined)
> and then any funcs called after that see the var bound to the
> new value, but then when the original block that had redefined
> it goes out of scope then the older original value is restored
> automatically.
>
> Can some clever use be made of bind and objects in some way?
> Can new instances of the functions be made and the words
> in the procedure bound to them?
> e.g. bind x,y,z to this "result", or bind copies of them?
>
> I thought already about creating an object which has a layer like
> this:
>
> r: make object! [
> parm1: result
> xx: x parm1
> yy: y parm1
> zz: z parm 1
> aappend: func[ str] [append parm1 str]
> ]
>
> and then going like this
> r
> r/xx
> r/yy
> r/zz
> r/aappend "stuff"
>
> but then I thought that it's only slightly better, and still not ideal.
> what would be really cool is a way to bind x, y, and z
> so that I don't need any extra clutter, so my code would just be
> x
> y
> z
> append "stuff"
>
> and all these operations would happen on "result"
>
> Does anybody have any idea what the heck I am trying to get at?
> I hate seeing oodles of functions where the same stupid parameter
> is passed over and over and over and over up and down thru the
> various levels, never changing, just getting passed again and again and again,
> and sometimes not just one but several. Ick. This situation must
> be very common.
>
> When we speak in human languages, we
> often introduce a subject or topic and then it's understood
> that we are referring to that until we say otherwise.
> Computers are very precise, but only through tedious programmer
> effort. The obvious stuff shouldn't have to be said, said, said, said.
>
> ?
>
> -galt
>
>
>