You can use bind to execute functions in a protected context.
I have done this in a compiler for a game scripting language. I could have
implemented the compiler with parse, but then I could not make use of Rebol
constructs within my custom game dialect. By making the game commands normal
functions the capabilities of Rebol can be leveraged to expand macros and
templates, evaluate mathematical expressions, do conditional compilation,
etc.
In order not to pollute the global namespace, these game commands are placed
within a context, and to compile a block of code in that context requires
bind.
REBOL []
game-script-compiler: context [
commands: context [
; These emit bytecode.
run: func [speed] [print ["emit run" speed "here"] ]
jump: func [height] [print ["emit jump" height "here"] ]
laugh: does [print "emit laugh here"]
]
set 'compile func [code] [
do bind code in commands 'run
]
]
compile [
run 1.4
jump 2.0
laugh
run 1.4 * 0.8 ; Rebol will evaluate this for us as normal.
]
-Karl
On Wednesday 21 January 2004 09:41 am, Andreas Bolka wrote:
> I will spare you my thoughts about REBOL's contexts and 'bind (for
> now) but I have a practical question:
>
> What is the utility of 'bind? No, I don't necessarily mean the typical
> cases where bind is needed to prevent errors. What I'm really thinking
> about are situations, where REBOL's behaviour regarding contexts and
> bind is actually contributing towards an elegant solution for a real
> problem.
>
> I think I remember various parse-based solutions posted to the list,
> that utilized bind. This is basically a question to those who
> understand _and_ use 'bind to actually solve problems: What do you use
> it for? Do you think REBOL's behaviour in those regards is practical?
> Are you aware of elegant solutions based on 'bind?
>
> --
> Best regards,
> Andreas
--
To unsubscribe from this list, just send an email to
[EMAIL PROTECTED] with unsubscribe as the subject.