In Forth Dimensions Vol 14 issue 1 (p. 37) there is an interesting piece of code to automatically assign enumerants:
: enum
create 0 ,
does> create dup @ 1 rot +! , does> @
;
So now instead of writing:
0 constant red 1 constant blue 2 constant green 0 constant round 1 constant square 2 constant oval
you can write:
enum color enum shape color red color blue color green shape round shape square shape oval
But its double does> is rather confusing. In fact the standard word constant makes things shorter:
: enum create 0 , does> dup @ 1 rot +! constant ;
The idiom dup @ 1 rot +! is similar to writing *x++ in C. It can be useful in other places, so a further factoring is:
: postinc ( a -- x ) \ increment a, return its original value dup @ 1 rot +! ; : enum create 0 , does> postinc constant ;