Name resolution (programming languages): Difference between revisions

Content deleted Content added
4-space indention for Java
Line 22:
<source lang="pycon">
>>> number = 99
>>> first_noun = "troublesproblems"
>>> second_noun = "hound"
>>> # Which variables to use are decided at runtime
>>> print(f"I got {number} {first_noun} but a {second_noun} ain't one.")
I got 99 troublesproblems but a hound ain't one.
 
</source>
Line 40:
 
For example, the parameter "foo" shadows the local variable "foo" in this common pattern:
<source lang="java">
private int foo; // Name "foo" is declared in the outer scope
 
public void setFoo(int foo) { // Name "foo" is declared in the inner scope, and is function-local.
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).
}
 
public int getFoo() {
return foo;
}
</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.