Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yes, defining words like "compute-numerator" does add entries to the dictionary, but that happens entirely at compile time. Forth doesn't insert a "compiled thunk" at runtime, the word is compiled as a name bound to a sequence of code field addresses (CFAs). When invoked, it's just a jump through the usual inner interpreter. There's no runtime cost for defining the word itself. When you invoke "compute-numerator" at runtime, the inner interpreter simply threads through those CFAs. There's no indirection, JIT, or dynamic thunk creation involved. The only runtime effect is the word being executed when called. All linking is resolved at compile time.

If you're concerned about polluting the global dictionary, a common idiom is (which you already know):

  \ Define and forget immediately if temporary
  : tmp-numerator a b + c d + * ;
  tmp-numerator
  FORGET tmp-numerator
or alternatively, you can isolate temporary definitions in a separate vocabulary:

  VOCABULARY TMP-WORDS
  TMP-WORDS DEFINITIONS

  : numerator   1 2 + 3 4 + * ;
  : denominator 5 6 + 7 8 + * ;

  ONLY FORTH ALSO TMP-WORDS ALSO DEFINITIONS

  : compute-ratio numerator denominator / . ;

  compute-ratio

  ONLY FORTH DEFINITIONS
TL;DR: Defining intermediate words adds entries to the dictionary, but this happens at compile time, not runtime. There's no additional runtime overhead. Naming conventions, FORGET, or vocabularies can mitigate dictionary pollution / clutter, but still, factoring remains the standard idiom in Forth.

Note: In some native code compiling or JIT-based Forth implementations, definitions may generate machine code or runtime objects rather than simple CFA chains I mentioned, but even in these cases, compilation occurs before runtime execution, and no dynamic thunk insertion happens during word calls.

I hope I understood your comment correctly. Please let me know!



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: