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
?
The short answer:
X
will outputOld Y
, see also your example in online test. At the time whenY
is defined in the second time, theX
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).