Defer

From RevaWiki

Jump to: navigation, search

Return to the main manual page


Glossary entry

Note: Reva now implements defer similarly to DOER in HelFORTH.

A "defer" allows you to define the functionality of a word later:

defer me
: show ." call me " me ;
| ... this an example, imagine a lot of code here
:: ." Helmar" ; is me

You might use this to declare a word now, and define how it works later on. This is similar to a "forward declaration" in other languages. (recall that in Forth, a word must be defined before it's referenced)

It is most useful, however, when one wishes to change the behavior of a word - whether permanently or temporarily. Reva (as of 6.0.4) supports both.

Words

The words you need to know are:

defer
Creates a new deferred word (a.k.a. doer). Initially, the word is set to noop, meaning it doesn't actually ... do ... anything
is
Assigns the behavior of the passed-in xt to the named doer, both as the default and as the current behavior
>defer
Assigns the passed-in xt to be the current behavior of the doer
undo
Restores the default behavior of the doer
make
Assigned an anonymous code chunk to be the current behavior of the doer

Examples

  • Simple and traditional defer/is:
defer joe
: aword ." hi there" cr ;
' aword is joe
  • Similarly, one can use is inside a colon-def:
: bword ['] aword is joe ;
  • Temporarily change the behavior:
' words >defer joe
  • Restore the default:
undo joe
  • Use make for the same purpose as ">defer":
make joe ." hi there" cr ;
  • Even inside a colon-def:
: aword make joe ." hi there" cr ;
Personal tools