Content deleted Content added
dedent code examples |
Jerryobject (talk | contribs) m WP:LINKs: update-standardizes, needless WP:PIPE > WP:NOPIPE. Cut needless carriage return in paragraph. |
||
(9 intermediate revisions by 9 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
==Overview==
Line 15 ⟶ 16:
In [[programming language]]s, name resolution can be performed either at [[compile time]] or at [[Run time (program lifecycle phase)|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
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 the [[Python (programming language)|Python]] interactive [[REPL]]:
<
>>> number = 99
>>> first_noun = "problems"
Line 28 ⟶ 29:
I got 99 problems but a hound ain't one.
</syntaxhighlight>
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|
Examples of languages that use static name resolution include [[C (programming language)|C]], [[C++]], [[E (programming language)|E]], [[Erlang (programming language)|Erlang]], [[
==Name masking==
Line 40 ⟶ 41:
For example, the parameter "foo" shadows the local variable "foo" in this common pattern:
<
private int foo; // Name "foo" is declared in the outer scope
Line 53 ⟶ 54:
return foo;
}
</syntaxhighlight>
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 [[
For example, in this code:
<
class Point {
private:
Line 75:
void setY(double newy) { y = newy; }
}
</syntaxhighlight>
within the
<
class Point {
private:
Line 91:
void setY(double newy) { y = newy; }
}
</syntaxhighlight>
In the new version, there is no masking, so it is immediately obvious which uses correspond to which declarations.
|