CallByThunk is a technique for implementing ParameterPassing where a thunk (a small function, usually emitted by the compiler, which returns the intended argument after doing necessary processing - see WhatIsaThunk) is passed from the caller to the callee. When the callee wants to evaluate the argument, it calls the thunk which returns the value.
(The returned value can be either the value or a reference; so CallByReference and CallByValue can be implemented on top of thunks; this distinction isn't very useful. A thunk could, I suppose, return another thunk; but I've never heard of that being done).
Consequences:
- Much slower than either CallByReference and CallByValue; if for no other reason than the overhead of the function call.
- Can support NormalOrderEvaluation and LazyEvaluation; the other forms can only support StrictEvaluation (unless functions or objects are passed).
- Often done for reasons not apparent to the programmer; such as converting pointer types or crossing some an address-space boundary.
- In OO and functional languages, the line between a thunk and a first-class function or object can often be blurry; see SmalltalkBlocksAreThunksInDisguise. (One potential difference; thunks usually are generated by the compiler, not written by hand; true thunks are seldom first-class entities).
- True thunks are found in AlgolSixty. (AnswerMe: Do HaskellLanguage and other non-strict functional languages, or delay'd parameters in SchemeLanguage, use thunks?) C compilers for some versions of Windows used thunks to allow interoperability between 16- and 32-bit code.
Note. The Algol mechanism is officially called
CallByName; which is a
logical parameter passing method (that describes the semantics of Algol functions).
CallByThunk is the underlying
implementation technique;
CallByThunk is used for many other purposes than implementing
CallByName.
Exactly. CallByThunk is not a ParameterPassing mode, it's a way to implement some parameter passing modes. Nobody talks about CallByThunk, or programs in terms of CallByThunk, they talk about and program in terms of, CallByReference, CallByValue, CallByName, etc.
See also ParameterPassing, CallByReference, CallByValue
CategoryLanguageFeature