Content deleted Content added
No edit summary |
Jerryobject (talk | contribs) m WP:LINKs: update-standardizes, needless WP:PIPE > WP:NOPIPE. Cut needless carriage return in paragraph. |
||
(25 intermediate revisions by 18 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'''
==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"
>>> print(f"I got {number} {first_noun} but a {second_noun} ain't one.")
</syntaxhighlight>
▲# which variables to use are decided at runtime
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]], [[
▲# outputs: 999 troubles and a hound ain't one
▲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|accessdate=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|accessdate=2011-01-23}}</ref> The feature also may be removed in a later version of Python.<ref>{{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}}</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]], [[REBOL]], and [[Tcl]].
==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 void 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.
|