World: r3wp
[Core] Discuss core issues
older newer | first last |
[unknown: 5] 20-Dec-2008 [11719x3] | Gregg, to reduce memory overhead and allow the garbage collector to give back some memory. |
Gregg, I don't think your second question matters to me. What matters is efficiency to me. | |
Sundanda , here is a test: | |
Geomol 20-Dec-2008 [11722] | I found a way to set all locals to none, while just specifying one of the locals, and it isn't beautiful, I'm afraid: f: func [/local a b c][ a: 1 b: 2 c: 3 print [a b c] ; This next line set all locals to none: set next bind first :f 'a none print [a b c] ] Running the function in the console: >> f 1 2 3 none none none >> |
[unknown: 5] 20-Dec-2008 [11723x4] | myfunc: func [ /local lcl lcl2][ lcl: "I still have a value" print mold lcl2: next find third get 'myfunc /local foreach item lcl2 [print mold item] foreach item lcl2 [print mold get :item] foreach item lcl2 [set :item none!] print lcl ] |
Grrr, I see something I did wrong in my test. | |
myfunc: func [ /local lcl lcl2][ lcl: "I still have a value" print mold lcl2: next find third get 'myfunc /local foreach item lcl2 [print mold item] foreach item lcl2 [print mold get :item] foreach item lcl2 [set :item none] print lcl ] | |
Yeah John, that is the way I think it has to be done by using 'bind. | |
Geomol 20-Dec-2008 [11727] | My example only works for functions, which doesn't take arguments. If a function take argument, you need: f: func [v /local a b c][ a: 1 b: 2 c: 3 print [a b c] set next find bind first :f 'a /local none print [a b c] ] |
[unknown: 5] 20-Dec-2008 [11728] | right |
Geomol 20-Dec-2008 [11729] | Hard to read that setting-to-none line, and I haven't found a way to do it by a function call (like a clear-locals function). |
[unknown: 5] 20-Dec-2008 [11730] | Its a good idea though - isn't it? |
Geomol 20-Dec-2008 [11731] | Yes, a native clear-locals seem like a good idea. |
[unknown: 5] 20-Dec-2008 [11732] | I agree |
BrianH 20-Dec-2008 [11733] | Remember, all of the parameters are locals, not just the ones after the /locals refinement. |
[unknown: 5] 20-Dec-2008 [11734] | yeah true. We should be setting the args to none also. |
BrianH 20-Dec-2008 [11735] | If you don't want to corrupt things before you return the value from the function, use ALSO - that is what it was designed for. |
[unknown: 5] 20-Dec-2008 [11736] | Yeah I love Also Brian and glad you implemented that. But this is really related to all the other non returned values. |
BrianH 20-Dec-2008 [11737] | R3 doesn't have this problem, so you are not likely to get a native. |
Geomol 20-Dec-2008 [11738] | :-) It gets worse, if all parameters should be set to none: f: func [v /local a b c][ a: 1 b: 2 c: 3 print [v a b c] set bind head remove find first :f /local 'a none print [v a b c] ] Try: >> f 42 |
BrianH 20-Dec-2008 [11739] | Keep in mind that unless you have a known name reference to a function, the code inside the function has no idea which function it is inside, so a native would need a reference to a function or a word with the function assigned to it or it won't work - natives aren't magic. |
Geomol 20-Dec-2008 [11740x3] | Hm, I don't need the HEAD, so it can be: set bind remove find first :f /local 'a none |
Brian, do you know, if it's possible from outside a function to specity its context? (To be used in the second parameter for BIND.) | |
*specify* | |
BrianH 20-Dec-2008 [11743] | You don't need the find either. John, your example with ALSO: f: func [v /local a b c][ a: 1 b: 2 c: 3 print [v a b c] also print [v a b c] set bind remove first :f 'a none ] |
Geomol 20-Dec-2008 [11744x3] | nah, doen't work. |
*doesn't* | |
:-) It has to find the /local and remove it. | |
[unknown: 5] 20-Dec-2008 [11747x2] | use difference |
difference first :f [/local] | |
BrianH 20-Dec-2008 [11749] | In answer to your question, in R2 it is only possible from outside a function to retrieve its context. You were doing so in your example. It doesn't have any meaning when the function isn't running, but you can't get a reference to the function from the inside. |
[unknown: 5] 20-Dec-2008 [11750x2] | But Brian, what if an other function is called inside the function your trying to release from? |
just as an ending line in your function that says: clear-locals this-function | |
Geomol 20-Dec-2008 [11752] | I was thinking about making a clear-locals function, but then I need to access the context of the function calling clear-locals (from clear-locals). |
BrianH 20-Dec-2008 [11753x2] | Remember, the /local refinement is also a local variable, so if you want to clear *all* the locals you also need to clear the word versions of the refinements. |
For the most part you don't need to clear *all* of the locals, just a select few that still refer to large series or structures. | |
[unknown: 5] 20-Dec-2008 [11755] | Brian, it is one of those "While were at it" scenarios |
BrianH 20-Dec-2008 [11756] | John, you can't just access the context of a calling function or even the current function, even from a native. The code block your code is in can be assigned to many functions, and isn't bound to any context. Words are direct bound individually. If you want to clear a context, yo have to specify which one in a parameter. |
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 [11768] | Sorry, I meant second. Second returns the code block. The first and third blocks are definitely *not* bound to the function's context. |
older newer | first last |