Word redefinition in Forth

580 views Asked by At

In Forth, in case of re-definition of word, what is the expected behavior of another word that uses the re-defined one? For e.g. if x calls y:

: Y ." Old Y " CR ;
: X 10 0 DO Y LOOP ;
\ ... 
: Y ." New Y " ;

then after the re-definition of Y, what should be the output of X, Old Y or New Y ?

1

There are 1 answers

2
ruvim On BEST ANSWER

The short answer: X will output Old Y, see also your example in online test. At the time when Y is defined in the second time, the X is already compiled.

In Forth, redefinition is just shadowing: it is the case when the name of a new definition shadows some another name in the same word list, and the shadowed definition becomes inaccessible (unfindable) by this name.

Also Forth uses incremental compilation and static name resolution (that is performed in compile time). As the result, the new definitions don't affect any previous definitions (and already compiled code).