For those curious about the true history, Carl was conflicted on the matter:
http://www.rebol.net/r3blogs/0025.html - "Leaky Functions"
Use LAMBDA.
>> add-twenty: lambda [x] [x + 20]
>> add-twenty 1000
== 1020
Things to know about LAMBDA:
-
It doesn't have a definitional RETURN at all. So if you execute a RETURN inside a lambda and it's not an error, that means it found someone else's return to run:
F: func [x] [ L: lambda [] [return x + 4] ; runs F's return L panic "unreachable" ] >> F 300 == 304 -
If you want to annotate LAMBDA for what it returns, the syntax is
[]: [<typespec>].add-twenty: lambda [ "Add twenty to the passed in integer." []: [integer!] x [integer!] ][ x + 20 ]It does look a little bit unusual. But it was chosen after a long amount of reflection. The empty block shows that there's no
RETURN:orYIELD:or other word in the frame that you can use to trigger a return...and it's especially good to reinforce that when you consider the effect I show above of "RETURN means someone else's RETURN".Perhaps you'll find it jarring at first, but give it a chance. It dovetails rather nicely with the "transparent" evaluator behavior of
[]:>> []: 10 == 10 >> []: pack [10 20] == \~['10 '20]~\ ; antiform (pack!) >> error? []: fail "HI" == \~okay~\ ; antiform (keyword!) -
If you don't have an interesting result, and don't want to document that by writing FUNC with
return: [trash!]you can usePROCEDURE(shorthanded asPROC):>> data: [] >> P: proc [msg] [append data msg] >> P "result of P will be squashed" ; no appended data returned >> data == ["result of P will be squashed"]PROCEDURE offers a RETURN. And to make generic macros or patterns easier you can pass it an argument... but if you pass it an argument it will be typechecked as TRASH!
The fact that the specific instances of the function argument variables are not available until each function instantiation is run makes this trickier than it first appears. Binding composition is just hard to begin with, and it gets harder when the things you're binding to don't exist yet at the time of composition.
But I find it encouraging that there are answers to binding problems in Ren-C (vs. just throwing up our hands and going "well, can't be done", which is pretty typical in other Redbols.)