Word redefinition in Forth

你离开我真会死。 提交于 2020-04-13 06:44:18

问题


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:


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).



来源:https://stackoverflow.com/questions/41487349/word-redefinition-in-forth

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!