Funarg problem: Difference between revisions

Content deleted Content added
Practical implications: More precise about Java.
Line 29:
 
==Practical implications==
Historically, the upwards funarg problem has proven to be the more difficult. For example, the [[Pascal programming language]] allows functions to be passed as arguments but not returned as results; thus implementations of Pascal are required to address the downwards funarg problem but not the upwards one. The [[Modula-2]] and [[Oberon programming language]]s (descendants of Pascal) allow functions both as parameters and return values but the assigned function may not be a nested function. The [[C (programming language)|C programming language]] historically avoids the main difficulty of the funarg problem by not allowing function definitions to be nested; because the environment of every function is the same, containing just the statically-allocated global variables and functions, a pointer to a function's code describes the function completely. [[Apple, Inc.|Apple]] has proposed and implemented a [[Blocks (C language extension)|closure syntax for C]] that solves the upwards funarg problem by dynamically moving closures from the stack to the heap as necessary.{{citation needed|date=November 2012}} The [[Java programming language]] deals with it by requiring that context used by nested functions in anonymous inner and local classes be declared [[Final (Java)|final]] and context used by [[Anonymous_function#Java|Lambda Expressions]] be effectively final. [[C_Sharp_(programming_language)|C#]] and [[D (programming language)|D]] have lambdas (closures) that encapsulate a function pointer and related variables.
 
In [[functional language]]s, functions are first-class values and can be passed anywhere. Thus, implementations of [[Scheme (programming language)|Scheme]] or [[SML programming language|SML]] must address both the upwards and downwards funarg problems. This is usually accomplished by representing function values as [[Dynamic memory allocation|heap-allocated]] closures, as previously described. The [[OCaml]] compiler employs a hybrid technique (based on [[program analysis (computer science)|program analysis]]) to maximize efficiency.{{Citation needed|date=April 2011}}