You. Forth. Simplicity.

git repository
download
prior releases
documentation wiki
issue tracker
mailing list and forum
irc channel and logs
try it


This wiki remains, like the original forum, mainly for historical value. There is a new wiki which, while having less content, is more secure and oriented towards modern Retro implementations.

reload node | edit | recent changes | front page

Vectored Words

Exposing words with alias or is works nicely, but can get messy if you want to expose several words in a scope or vocabulary. One way around this is to use vectored words. These are similar to, but a little more flexible than, deferred words in ANS Forth. A vectored word will have vector as the first word in its definition. This is the only step necessary to make a word into a vector. Three words are provided for altering and resetting vectored definitions. These are is, default: and devector.

The is word is used to set a vector to a new xt. In use it takes the new xt off the stack and parses for the name of the vectored word to change. If the specified name does not exist, it creates a new vectored word with a default definition calling the specified XT. To reset a changed vector to the original definition, the word devector is used. This parses ahead for the name of the word to remove the vector from.

default: is used in certain cases where you have a vector vector sequence. This word changes the second vector.

What these four words allow is the changing of existing definitions. As a simple example, try the code below.

: a vector ;
: b vector ;
: c vector ;
: d a b c ;
d .s reset
:: 1 ; is a
d .s reset
:: 2 ; is b
d .s reset
' + is c
d .s reset

While a bit simplistic, this does show how to use vectors. Learning to use vectored words will allow you to explore new options in your programs.


One useful form of vector is create named functions which can be assigned within a loc pair. The name and initial definition of a function can be placed in the dictionary, and later changed to a more complex definition within the loc pair (within which all newly created words lose their names as soon as it closes.)

Example:
: foo vector ;
loc:
10 constant a
20 constant b
here is foo ] a b * ;
;loc`

See also: Anonymous words.