词条 | Non-local variable |
释义 |
In programming language theory, a non-local variable is a variable that is not defined in the local scope. While the term can refer to global variables, it is primarily used in the context of nested and anonymous functions where some variables can be neither in the local nor the global scope. In Lua they are called the upvalues of the function.[1] == Examples == === Nested functions === In the Python 3 example that follows there is a nested function In Javascript, the locality of a variable is determined by the closest === Anonymous functions === In the Haskell example that follows the variable Implementation issues{{see also|Nested function#Implementation|Man or boy test}}Non-local variables are the primary reason it is difficult to support nested, anonymous, higher-order and thereby first-class functions in a programming language. If the nested function or functions are (mutually) recursive, it becomes hard for the compiler to know exactly where on the call stack the non-local variable was allocated, as the frame pointer only points to the local variable of the nested function itself and there can be an arbitrary number of activation records on the stack in between. This is generally solved using access links or display registers. If the nested function is passed as an argument to a higher-order function a closure needs to be built in order to locate the non-local variables. If the nested function is returned as a result from its outer function (or stored in a variable) the non-local variables will no longer be available on the stack. They need to be heap allocated instead, and their lifetime extend beyond the lifetime of the outer function that declared and allocated them. This generally requires garbage-collection. Notes1. ^Programming in Lua (first edition), "27.3.3 – Upvalues" References
2 : Programming language theory|Variable (computer science) |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。