简体   繁体   中英

Word redefinition in Forth

In Forth, in case of re-definition of word, what is the expected behavior of another word that uses the re-defined one? For eg 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 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM