Just starting learning Forth. While practicing I defined a word I named min
. It turns out that min
is a built-in word. Now when I try to find the minimum of two numbers my own min
runs!
Is there a way to avoid this from happening again? How do I get back the built-in min
?
Thanks.
EDIT
I quit the terminal and the built-in min
is restored.
So, how do I avoid defining words with names that conflict with built-in words?
how do I avoid defining words with names that conflict with built-in words?
It depends on what you want the Forth system do when you try to redefine a word. And should this apply to cases where you are redefining your own word.
Forth doesn't actually distinguish between user-defined words and built-in words (including standard words).
Typically, Forth systems give a warning when you redefine a word in the same word list. Otherwise, even if the new definition shadows a word in another word list in the search order (or, conversely, a word in another word list in the search order shadows the new definition) — you do not get any warning.
A simplest way to prevent redefinitions of the latter kind is to redefine the defining words to check the corresponding condition.
An example for the word ":
" (colon):
: : ( "name" -- colon-sys )
>in @ >r \ save the position in the input buffer
parse-name find-name ( nt | 0 )
abort" A word with the given name is available in the search order"
r> >in ! \ restore the position in the input buffer
: \ call to the original ":"
;
Once you enter this definition, you will not be able to create a new definition for a name using :
(colon) if a word with the same name is available in the search order. But you will still be able to redefine a word in a word list that is absent in the search order.
Also, you will be able to redefine a word using other defining words like defer
, create
, variable
, etc. All of them can also be redefined in the way as to prevent further redefinitions of the mentioned kind. Of course, you should do it before redefining :
(colon).