r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Core] Discuss core issues

Steeve
20-Dec-2008
[11757x4]
fun: func [spec body /local me][
	me: construct append copy/deep [
		clear-all: [set bind copy next next first self self none]
	] body	
	me/clear-all: does me/clear-all
	clear find spec /local
	func spec bind body me 
]

f: fun [/local c d ][ c: d: 1 probe self print [c d ] clear-all print 
[c d]]
f
a function with a context for local vars
but not perfect, all vars which are set (including globals) are redefined 
as locals
but there is an idea
BrianH
20-Dec-2008
[11761]
You can't get access to a function's context without having a known 
word that is already bouund to that context. When you do first :f, 
the words retrn are not bound to the function's context. In R2, when 
you do third :f you get words that are bound, but you can only guess 
to what., since words are bound individually. The only safe way to 
do clear-words is to pass both the function and a known word bound 
to the function's context, and even then you have to copy the fuunction's 
argument block and convert any refinements to words before you bind 
the copy.
[unknown: 5]
20-Dec-2008
[11762x4]
exactly
that is why I'm attempting this with third and using clear-locals 
:func :arg
actually :fnc
not got it yet though
BrianH
20-Dec-2008
[11766]
You can't assme that any of the words in the block retuurned by third 
are bound to the function's context, even if  they are spelled the 
same as argument words or refinements.
[unknown: 5]
20-Dec-2008
[11767]
why not?
BrianH
20-Dec-2008
[11768x2]
Sorry, I meant second. Second returns the code block. The first and 
third blocks are definitely *not* bound to the function's context.
I mean the words in those blocks. The blocks themselves can't be 
bound at all - only words can be bound.
[unknown: 5]
20-Dec-2008
[11770]
So brianH are you saying it is impossible in R2 to send a function 
within a func to clear the parent functions locals?
BrianH
20-Dec-2008
[11771x4]
No, just difficult unless you provide 2 parameters:
- A reference to the function or a copy of its parameter list.
- A word known to be bound to the function's context.
Even then, it is not very efficient. You are better off clearing 
select words explicitly.
This is part of why R3's function contexts are different: to make 
this kind of thing unnecessary.
As a side effect, the clear-words function would be impossible in 
R3.
[unknown: 5]
20-Dec-2008
[11775]
What part would be inefficient?
BrianH
20-Dec-2008
[11776]
Getting a the argument list, which you would have to copy, convert 
refinements to words, then bind. The copy and conversion would be 
slower than necessary.
[unknown: 5]
20-Dec-2008
[11777x3]
Yeah I don't know if the overhead is worth it.
R3 in 09?
Hmm..  I should sell T-shirts with that on it.
BrianH
20-Dec-2008
[11780]
Developer releases at least, and maybe before then.
[unknown: 5]
20-Dec-2008
[11781x4]
Everyone would ask "What is R3?" and then we could tell them about 
REBOL.
>> myfunc: func [arg /local lcl lcl2][
[        lcl: "I still have a value"
[        lcl2; 2
[        clear-locals :myfunc lcl
[        print lcl
[        print lcl2
[    ]
>>
>> myfunc 2
none
none
>>
typo in my function
>> myfunc: func [arg /local lcl lcl2][
[        lcl: "I still have a value"
[        lcl2: 2
[        clear-locals :myfunc lcl
[        print lcl
[        print lcl2
[    ]
>>
>> myfunc 2
none
none
Steeve
20-Dec-2008
[11785x2]
clearly guys, a function should never be so huge and obscufated so 
that it will not be any more interesting to use a clear-locals func.
i meant, it's better to do a my-var: none when necessary
[unknown: 5]
20-Dec-2008
[11787x2]
I disagree.
I have 22 locals in one function alone already and even though I 
plan on reducing some of those in reducing some code, I have a feeling 
that I will regain them as I add more code in the function.
Steeve
20-Dec-2008
[11789x3]
for this specific case (lot of locals). you could define a local 
block to reset them simply.
f: func [ ... /local a f g...][
	local: [a f g ...]
	....
	set local none		;clear locals
]
it's enough work
did you know that 'set still works with block of get-words [:a :b 
:c]
[unknown: 5]
20-Dec-2008
[11792x2]
REBOL has all kinds of niffty stuff ;-)
What is happening with respect to memory and lower level activity 
when using the 'bind function?
[unknown: 5]
21-Dec-2008
[11794x2]
As for the clearing of the locals  from a function - here you go:

clear-locals: func [

        {Used as the ending statement within a function to set the function's 
        locals to none value.}
        fnc [function!]
    ][

        set bind first to-block trim/with mold first :fnc "/" first find 
        second :fnc set-word! none
]
Would make a good mezz for 2.7.7
Anton
21-Dec-2008
[11796x2]
It can't be made to always work.
There may not be a set-word in the function body.
[unknown: 5]
21-Dec-2008
[11798x2]
Anton, in that case wouldn't wouln't have a need to use a clear-locals 
function.
See the answers group as Gabriele posted a superior clear-locals 
function.
Anton
21-Dec-2008
[11800]
Yep, Gabriele got it.
BrianH
21-Dec-2008
[11801]
Note that his solution requires the known word parameter, but not 
the function context parameter. This means that it is more easy to 
call from "inside" the function where you can specify a known word 
instead of having to find one. The no-parameter solution is still 
up in the air :)
Anton
21-Dec-2008
[11802x5]
Just make your own function generator. You can basically inline clear-locals 
into the end of the function body.
There may be some tricky issues to take care of. Refer to Ladislav's 
documents where he does this kind of thing.
Ahh - tricky issue will be how to avoid upsetting the return value. 
Now it looks hard again.
No, it should be easy, just use ALSO with the whole body block.
No, using ALSO isn't so straighforward - it's hard again. :)