词条 | Name resolution (programming languages) |
释义 |
In programming languages, name resolution refers to the resolution of the tokens within program expressions to the intended program components. OverviewExpressions in computer programs reference variables, data types, functions, classes, objects, libraries, packages and other entities by name. In that context, name resolution refers to the association of those not-necessarily-unique names with the intended program entities. The algorithms that determine what those identifiers refer to in specific contexts are part of the language definition. The complexity of these algorithms is influenced by the sophistication of the language. For example, name resolution in assembly language usually involves only a single simple table lookup, while name resolution in C++ is extremely complicated as it involves:
Static versus dynamicIn programming languages, name resolution can be performed either at compile time or at runtime. The former is called static name resolution, the latter is called dynamic name resolution. A somewhat common misconception is that dynamic typing implies dynamic name resolution. However, static typing does imply static name resolution. For example, Erlang is dynamically typed but has static name resolution. Static name resolution catches, at compile time, use of variables that are not in scope; preventing programmer errors. Languages with dynamic scope resolution sacrifice this safety for more flexibility; they can typically set and get variables in the same scope at runtime. For example, in Python: However, relying on dynamic name resolution in code is discouraged by the Python community.[1][2] The feature also may be removed in a later version of Python.[3] Examples of languages that use static name resolution include C, C++, E, Erlang, Haskell, Java, Pascal, Scheme, and Smalltalk. Examples of languages that use dynamic name resolution include some Lisp dialects, Perl, PHP, Python, REBOL, and Tcl. Name maskingMasking occurs when the same identifier is used for different entities in overlapping lexical scopes. At the level of variables (rather than names), this is known as variable shadowing. An identifier I' (for variable X') masks an identifier I (for variable X) when two conditions are met
The outer variable X is said to be shadowed by the inner variable X'. For example, the parameter x masks the local variable in this common pattern: Name masking can cause complications in function overloading, due to overloading not happening across scopes in some languages, notably C++, thus requiring all overloaded functions to be redeclared or explicitly imported into a given namespace. Alpha renaming to make name resolution trivialIn programming languages with lexical scoping that do not reflect over variable names, α-conversion (or α-renaming) can be used to make name resolution easy by finding a substitution that makes sure that no variable name masks another name in a containing scope. Alpha-renaming can make static code analysis easier since only the alpha renamer needs to understand the language's scoping rules. For example, in this code: within the Point constructor, the class variables x and y are shadowed by local variables of the same name. This might be alpha-renamed to: In the new version, there is no masking, so it is immediately obvious which uses correspond to which declarations. See also
References1. ^{{cite web|url=http://mail.python.org/pipermail/python-ideas/2009-May/004582.html|title=[Python-Ideas] str.format utility function|date=9 May 2009|accessdate=2011-01-23}} {{DEFAULTSORT:Name resolution}}2. ^{{cite web|url=http://diveintopython.org/html_processing/dictionary_based_string_formatting.html|title=8.6. Dictionary-based string formatting|work=diveintopython.org|publisher=Mark Pilgrim|accessdate=2011-01-23}} 3. ^{{cite web |url=https://docs.python.org/tutorial/classes.html#python-scopes-and-namespaces|title=9. Classes - Python v2.7.1 documentation |quote=search for names is done dynamically, at run time — however, the language definition is evolving towards static name resolution|accessdate=2011-01-23}} 2 : Computer libraries|Compiler construction |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。