In section 4 of Starting Forth tutorial, the answer to the 5th question is:
: STAR [CHAR] * EMIT ;
: STARS ( #stars -- ) 0 ?DO STAR LOOP ;
: STARS ( n -- ) ?DUP IF STARS THEN ;
The third definition redefines STARS
word using previous definition.
Interpreter says:
redefined STARS ok
and as I type see
:
SEE STARS
: STARS
?dup
IF STARS
THEN ; ok
Why the former definition is still alive as the word STARS
has been replaced in the words dictionary? And is it possible to access/see the former definition?
The second definition of STARS
hasn't replaced the first definition. It has hidden it. The second definition calls the first definition.
words
STARS STARS STAR mov-regv-Iv mov-reg8-Ib xchg-ax jcc-short conditions...
\ Stars appears twice in the words listing
The forth interpreter searches for the latest definition of a word in the dictionary. In this case once it finds STARS
it stops looking any further.
: STAR [CHAR] * EMIT ;
: (STARS) ( #stars -- ) 0 ?DO STAR LOOP ;
: STARS ( n -- ) ?DUP IF (STARS) THEN ;
see stars
: STARS
?dup
IF (STARS)
THEN ; ok
see (stars)
: (STARS)
0
?DO STAR
LOOP
; ok
words
STARS (STARS) STAR mov-regv-Iv mov-reg8-Ib ...
By naming the first STARS
as something else, here (STARS)
, the first definition can still be accessed.
This hiding can be useful with tables of constant data. I think in 'Starting Forth' there's an example of a sine lookup table. The table is called sine and the word to access the table is also called sine. This means nobody can accidentally write into the sine table after the second sine is defined.