Semantics and Optimization of Copying Function Bodies

In order to get leverage out of my FENCE!-as-body-gathers-locals-at-top-level, it means that FUNC (and other generators) have to take the body argument literally, to examine it before it would run the default wrapping.

At first I thought "Ah shucks. Guess if you build the body via code you have to put it in a group. Some things just require compromises."

foo: func [a b] compose [return a ('+) b]
=>
foo: func [a b] (compose [return a ('+) b])

Then I realized: no, this is GREAT!

Because with the advantage of knowing it's a literal reference, we don't have to copy the function body if it is passed via a literal block.

So therefore...

body: [return a + b]
f1: func [a b] (body)  ; let's say group copies by default
append body spread [+ c]
f2: func [a b c] (body)  ; this is why copy by default for group

f3: func [a b] [return a + b]  ; but here, WHY COPY?
                               ; no one is referring to it!

Then I thought: would be nice to give an option to say "I know I gave you a GROUP!, but this code I'm passing you isn't imaged anywhere else, don't waste time with a copy".

That could be a refinement, but Ren-C can avoid bloating the frame for FUNC by just quoting the block: '[return a + b]

func [a b] (quote compose [return a ('+) b])  ; won't copy!
1 Like