Name resolution (programming languages): Difference between revisions

Content deleted Content added
m Static versus dynamic: grammar/flow
m WP:LINKs: update-standardizes, needless WP:PIPE > WP:NOPIPE. Cut needless carriage return in paragraph.
 
(8 intermediate revisions by 8 users not shown)
Line 1:
{{Short description|Matching of lexical tokens to the components of a computer program}}
{{see also|Name resolution (computer systems)}}
In [[programming language]]s, '''name resolution''' is the resolution of the tokens[[lexical analysis|token]]s within program expressions to the intended program components.
 
==Overview==
Line 20 ⟶ 21:
 
For example, in the [[Python (programming language)|Python]] interactive [[REPL]]:
<sourcesyntaxhighlight lang="pycon">
>>> number = 99
>>> first_noun = "problems"
Line 28 ⟶ 29:
I got 99 problems but a hound ain't one.
 
</syntaxhighlight>
</source>
However, relying on dynamic name resolution in code is discouraged by the Python community.<ref>{{cite web|url=http://mail.python.org/pipermail/python-ideas/2009-May/004582.html|title=<nowiki>[</nowiki>Python-Ideas<nowiki>]</nowiki> str.format utility function|date=9 May 2009|accessdateaccess-date=2011-01-23}}</ref><ref>{{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|accessdateaccess-date=2011-01-23}}</ref> The feature also may be removed in a later version of Python.<ref>{{cite web |url=https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces|title=9. Classes - Python documentation |quote=It is important to realize that scopes are determined textually: the global scope of a function defined in a module is that module’s namespace, no matter from where or by what alias the function is called. On the other hand, the actual search for names is done dynamically, at run time — however, the language definition is evolving towards static name resolution, at “compile”"compile" time, so don’t rely on dynamic name resolution! (In fact, local variables are already determined statically.)|accessdateaccess-date=2019-07-24}}</ref>
 
Examples of languages that use static name resolution include [[C (programming language)|C]], [[C++]], [[E (programming language)|E]], [[Erlang (programming language)|Erlang]], [[Haskell (programming language)|Haskell]], [[Java (programming language)|Java]], [[Pascal (programming language)|Pascal]], [[Scheme (programming language)|Scheme]], and [[Smalltalk]]. Examples of languages that use dynamic name resolution include some [[Lisp (programming language)|Lisp]] dialects, [[Perl]], [[PHP]], [[Python (programming language)|Python]], [[REBOLRebol]], and [[Tcl]].
 
==Name masking==
Line 40 ⟶ 41:
 
For example, the parameter "foo" shadows the local variable "foo" in this common pattern:
<sourcesyntaxhighlight lang="java">
private int foo; // Name "foo" is declared in the outer scope
 
Line 53 ⟶ 54:
return foo;
}
</syntaxhighlight>
</source>
Name masking can cause [[Function overloading#Complications|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 trivial==
In programming languages with [[lexical scoping]] that do not [[reflectionReflective (computer science)programming|reflect]] over variable names, [[Lambda calculus#.CE.B1-conversion|α-conversion]] (or α-renaming) can be used to make name resolution easy by finding a substitution that makes sure that no variable name [[variable shadowing|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.
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:
<sourcesyntaxhighlight lang="cpp">
class Point {
private:
Line 75:
void setY(double newy) { y = newy; }
}
</syntaxhighlight>
</source>
within the <tt>{{mono|Point</tt>}} constructor, the class[[instance variablesvariable]]s <tt>{{mono|x</tt>}} and <tt>{{mono|y</tt>}} are [[Variable shadowing|shadowed]] by local variables of the same name. This might be alpha-renamed to:
<sourcesyntaxhighlight lang="cpp">
class Point {
private:
Line 91:
void setY(double newy) { y = newy; }
}
</syntaxhighlight>
</source>
In the new version, there is no masking, so it is immediately obvious which uses correspond to which declarations.