赛派号

不锈钢用什么焊条焊接最好 Non

In computer programming, a variable which is not defined in the local scope This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: "Non-local variable" – news · newspapers · books · scholar · JSTOR (January 2025) (Learn how and when to remove this message)

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 in neither the local nor the global scope.

In Lua they are called the upvalues of the function.[1]

Examples[edit] Nested functions[edit]

In the Python 3 example that follows there is a nested function inner defined in the scope of another function outer. The variable x is local to outer, but non-local to inner (nor is it global):

from typing import Callable def outer() -> Callable[[], None]: x: int = 1 def inner() -> None: nonlocal x x += 1 print(x) return inner

In JaScript, the locality of a variable is determined by the closest var statement for this variable. In the following example, x is local to outer as it contains a var x statement, while inner doesn't. Therefore, x is non-local to inner:

function outer() { var x = 1; function inner() { x += 1; console.log(x); } return inner; } Anonymous functions[edit]

In the Haskell example that follows the variable c is non-local in the anonymous function \x -> x + c:

outer = let c = 1 in map (\x -> x + c) [1, 2, 3, 4, 5] Implementation issues[edit] See also: Nested function § Implementation, and 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 ailable on the stack. They need to be heap allocated instead, and their lifetime extends beyond the lifetime of the outer function that declared and allocated them. This generally requires garbage-collection.

Notes[edit] ^ Programming in Lua (first edition), "27.3.3 – Upvalues" References[edit] Aho, Lam, Sethi, and Ullman. "7.3 Access to Nonlocal Data on the Stack". Compilers: Principles, Techniques, & Tools. Second edition.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lsinopec@gmail.com举报,一经查实,本站将立刻删除。

上一篇 没有了

下一篇没有了