Skip to content
Stephen Crowley edited this page Dec 11, 2023 · 2 revisions

The term "thunk" in programming has several meanings, each evolving from its original concept introduced in 1961. Let's explore these meanings:

  1. Original Concept (1961): Invented by P. Z. Ingerman, a thunk originally referred to a piece of coding that provided an address. This was particularly relevant in ALGOL 60 procedure calls, where thunks were used to bind actual parameters to their formal definitions. If a procedure was called with an expression instead of a formal parameter, the compiler would generate a thunk to compute the expression and store the result's address in a standard location.

  2. Generalized Meaning: Later, the term expanded to describe an expression frozen with its environment (variable values) for later evaluation when needed, akin to a closure. The process of evaluating these thunks is known as "forcing".

  3. Stubroutine in Overlay Programming: In overlay programming environments, a thunk can also refer to a stubroutine that loads and jumps to the correct overlay.

  4. Comparison with Trampoline: Thunks are sometimes compared to trampolines in programming, although they serve different purposes.

The term "thunk" has some onomatopoeic myths regarding its origin, such as being the sound of data hitting the stack or an accumulator, or the sound of an expression being unfrozen at argument-evaluation time. However, its inventors named it after realizing that the type of an argument in ALGOL 60 could be pre-determined with some compile-time consideration, thereby simplifying the evaluation machinery. The term was playfully chosen as the past tense of "think" during a late-night discussion, indicating that it had "already been thought of".

Clone this wiki locally