Knowledge

Non-local variable

Source ๐Ÿ“

406:
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 extends beyond
448: 470: 465: 407:
the lifetime of the outer function that declared and allocated them. This generally requires garbage-collection.
372: 20: 387:
only points to the local variable of the nested function itself and there can be an arbitrary number of
403: 55: 28: 351: 361: 365: 8: 360:
Non-local variables are the primary reason it is difficult to support nested, anonymous,
40: 388: 396: 355: 36: 32: 459: 402:
If the nested function is passed as an argument to a higher-order function a
384: 48: 16:
In computer programming, a variable which is not defined in the local scope
446:
Aho, Lam, Sethi, and Ullman. "7.3 Access to Nonlocal Data on the Stack".
392: 44: 424: 380: 162:
In Javascript, the locality of a variable is determined by the closest
430: 376: 75:
In the Python 3 example that follows there is a nested function
391:
on the stack in between. This is generally solved using
166:
statement for this variable. In the following example,
371:If the nested function or functions are (mutually) 282:In the Haskell example that follows the variable 457: 27:is a variable that is not defined in the local 449:Compilers: Principles, Techniques, & Tools 383:the non-local variable was allocated, as the 43:where some variables can be in neither the 79:defined in the scope of another function 35:, it is primarily used in the context of 345: 286:is non-local in the anonymous function 458: 277: 182:doesn't. Therefore, x is non-local to 352:Nested function ยง Implementation 70: 13: 425:Programming in Lua (first edition) 14: 482: 417: 31:. While the term can refer to 1: 440: 379:to know exactly where on the 7: 471:Variable (computer science) 466:Programming language theory 368:in a programming language. 65: 21:programming language theory 10: 487: 375:, it becomes hard for the 349: 410: 292: 188: 97: 366:first-class functions 346:Implementation issues 95:(nor is it global): 58:they are called the 278:Anonymous functions 91:, but non-local to 41:anonymous functions 389:activation records 25:non-local variable 452:. Second edition. 431:27.3.3 โ€“ Upvalues 397:display registers 178:statement, while 174:as it contains a 62:of the function. 478: 434: 421: 341: 338: 335: 332: 329: 326: 323: 320: 317: 314: 311: 308: 305: 302: 299: 296: 289: 285: 273: 270: 267: 264: 261: 258: 255: 252: 249: 246: 243: 240: 237: 234: 231: 228: 225: 222: 219: 216: 213: 210: 207: 204: 201: 198: 195: 192: 185: 181: 177: 173: 169: 165: 158: 155: 152: 149: 146: 143: 140: 137: 134: 131: 128: 125: 122: 119: 116: 113: 110: 107: 104: 101: 94: 90: 86: 82: 78: 71:Nested functions 33:global variables 486: 485: 481: 480: 479: 477: 476: 475: 456: 455: 443: 438: 437: 422: 418: 413: 358: 356:Man or boy test 348: 343: 342: 339: 336: 333: 330: 327: 324: 321: 318: 315: 312: 309: 306: 303: 300: 297: 294: 287: 283: 280: 275: 274: 271: 268: 265: 262: 259: 256: 253: 250: 247: 244: 241: 238: 235: 232: 229: 226: 223: 220: 217: 214: 211: 208: 205: 202: 199: 196: 193: 190: 183: 179: 175: 171: 167: 163: 160: 159: 156: 153: 150: 147: 144: 141: 138: 135: 132: 129: 126: 123: 120: 117: 114: 111: 108: 105: 102: 99: 92: 88: 84: 83:. The variable 80: 76: 73: 68: 17: 12: 11: 5: 484: 474: 473: 468: 454: 453: 442: 439: 436: 435: 415: 414: 412: 409: 347: 344: 293: 288:\x -> x + c 279: 276: 189: 98: 72: 69: 67: 64: 15: 9: 6: 4: 3: 2: 483: 472: 469: 467: 464: 463: 461: 451: 450: 445: 444: 432: 428: 426: 420: 416: 408: 405: 400: 398: 394: 390: 386: 385:frame pointer 382: 378: 374: 369: 367: 363: 357: 353: 291: 187: 96: 63: 61: 57: 52: 50: 46: 42: 38: 34: 30: 26: 22: 447: 423: 419: 401: 393:access links 370: 364:and thereby 362:higher-order 359: 281: 170:is local to 161: 87:is local to 74: 59: 53: 49:global scope 24: 18: 460:Categories 441:References 381:call stack 350:See also: 373:recursive 377:compiler 218:function 191:function 127:nonlocal 66:Examples 60:upvalues 47:nor the 404:closure 242:console 354:, and 263:return 154:return 37:nested 411:Notes 328:-> 295:outer 266:inner 221:inner 194:outer 184:inner 180:inner 176:var x 172:outer 157:inner 142:print 121:inner 103:outer 93:inner 89:outer 81:outer 77:inner 45:local 29:scope 39:and 23:, a 395:or 316:map 301:let 248:log 203:var 164:var 124:(): 118:def 106:(): 100:def 56:Lua 54:In 19:In 462:: 399:. 313:in 290:: 257:); 233:+= 224:() 197:() 186:: 136:+= 51:. 433:" 429:" 427:, 340:) 337:c 334:+ 331:x 325:x 322:\ 319:( 310:1 307:= 304:c 298:= 284:c 272:} 269:; 260:} 254:x 251:( 245:. 239:; 236:1 230:x 227:{ 215:; 212:1 209:= 206:x 200:{ 168:x 151:) 148:x 145:( 139:1 133:x 130:x 115:1 112:= 109:x 85:x

Index

programming language theory
scope
global variables
nested
anonymous functions
local
global scope
Lua
Nested function ยง Implementation
Man or boy test
higher-order
first-class functions
recursive
compiler
call stack
frame pointer
activation records
access links
display registers
closure
Programming in Lua (first edition)
27.3.3 โ€“ Upvalues
Compilers: Principles, Techniques, & Tools
Categories
Programming language theory
Variable (computer science)

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.

โ†‘