Content deleted Content added
Jerryobject (talk | contribs) m WP:LINKs: update-standardizes, needless WP:PIPE > WP:NOPIPE. Cut needless carriage return in paragraph. |
|||
(21 intermediate revisions by 15 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'''
▲In [[programming language]]s, '''name resolution''' refers to the resolution of the tokens within program expressions to the intended program components.
==Overview==
Line 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
>>>
>>>
>>> #
>>> print(f"I got {
</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:
The outer variable X is said to be ''shadowed'' by the inner variable X'.
For example, the parameter
<
public void setFoo(int foo) { // A declaration with the same name in the inner scope▼
this.foo = foo;▼
public int getFoo() { return foo; }▼
▲
this.foo = foo; // Since "foo" will be first found (and resolved) in the ''innermost'' scope,
// in order to successfully overwrite the stored value of the attribute "foo"
// with the new value of the incoming parameter "foo", a distinction is made
// between "this.foo" (the object attribute) and "foo" (the function parameter).
}
}
</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:
<
public:▼
setX(x);▼
setY(y);▼
void setX(double newx) { x = newx; }▼
Point(double x,
}
within the <tt>Point</tt> constructor, the class variables <tt>x</tt> and <tt>y</tt> are [[Variable shadowing|shadowed]] by local variables of the same name. This might be alpha-renamed to:▼
<source lang="cpp">▼
class Point {▼
private:▼
double x, y;▼
public:▼
void
}
setX(a);▼
</syntaxhighlight>
setY(b);▼
▲within the
void setX(double newx) { x = newx; }▼
Point(double a,
}
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.
|