Download Understanding Scope and Namespaces in Modern Programming Languages and more Slides Advanced Computer Programming in PDF only on Docsity!
Scope
Chapter Ten Modern Programming Languages, 2nd ed. (^) Docsity.com 1
Reusing Names
Scope is trivial if you have a unique name
for everything:
But in modern languages, we often use the
same name over and over:
How can this work?
Chapter Ten Modern Programming Languages, 2nd ed. 2
fun square n = n * n; fun double n = n + n;
fun square a = a * a; fun double b = b + b;
Docsity.com
Definitions
When there are different variables with the
same name, there are different possible
bindings for that name
Not just variables: type names, constant
names, function names, etc.
A definition is anything that establishes a
possible binding for a name
Chapter Ten Modern Programming Languages, 2nd ed. (^) Docsity.com 4
Examples
Chapter Ten Modern Programming Languages, 2nd ed. 5
fun square n = n * n; fun square square = square * square;
const Low = 1; High = 10; type Ints = array [Low..High] of Integer; var X: Ints;
Docsity.com
Examples
Each occurrence must be bound using one
of the definitions
Which one?
There are many different ways to solve this
scoping problem
Chapter Ten Modern Programming Languages, 2nd ed. 7
- fun square square = square * square; val square = fn : int -> int - square 3; val it = 9 : int
Docsity.com
Outline
Definitions and scope
Scoping with blocks
Scoping with labeled namespaces
Scoping with primitive namespaces
Dynamic scoping
Separate compilation
Chapter Ten Modern Programming Languages, 2nd ed. (^) Docsity.com 8
Different ML Blocks
The let is just a block: no other purpose
A fun definition includes a block:
Multiple alternatives have multiple blocks:
Each rule in a match is a block:
Chapter Ten Modern Programming Languages, 2nd ed. 10
fun cube x = xxx;**
fun f (a::b::_) = a+b | f [a] = a | f [] = 0;
case x of (a,0) => a | (_,b) => b
Docsity.com
Java Blocks
In Java and other C-like languages, you can
combine statements into one compound
statement using { and }
A compound statement also serves as a
block:
Chapter Ten Modern Programming Languages, 2nd ed. 11
while (i < 0) { int c = iii; p += c; q += c; i -= step; }** Docsity.com
Classic Block Scope Rule
The scope of a definition is the block
containing that definition, from the point of
definition to the end of the block, minus the
scopes of any redefinitions of the same
name in interior blocks
That is ML’s rule; most statically scoped,
block-structured languages use this or some
minor variation
Chapter Ten Modern Programming Languages, 2nd ed. (^) Docsity.com 13
Example
Chapter Ten Modern Programming Languages, 2nd ed. 14
let val n = 1 in let val n = 2 in n end end
Scope of this definition is A-B
Scope of this definition is B
A
B
Docsity.com
Labeled Namespaces
A labeled namespace is any language
construct that contains definitions and a
region of the program where those
definitions apply, and also has a name that
can be used to access those definitions from
outside the construct
ML has one called a structure…
Chapter Ten Modern Programming Languages, 2nd ed. (^) Docsity.com 16
ML Structures
A little like a block: a can be used
anywhere from definition to the end
But the definitions are also available
outside, using the structure name: Fred.a
and Fred.f
Chapter Ten Modern Programming Languages, 2nd ed. 17
structure Fred = struct val a = 1; fun f x = x + a; end;
Docsity.com
Example
The variables min and max would be
visible within the rest of the class
Also accessible from outside, as
Month.min and Month.max
Classes serve a different purpose too
Chapter Ten Modern Programming Languages, 2nd ed. 19
public class Month { public static int min = 1; public static int max = 12; … }
Docsity.com
Namespace Advantages
Two conflicting goals:
- Use memorable, simple names like max
- For globally accessible things, use uncommon
names like maxSupplierBid , names that
will not conflict with other parts of the program
With namespaces, you can accomplish both:
- Within the namespace, you can use max
- From outside, SupplierBid.max
Chapter Ten Modern Programming Languages, 2nd ed. (^) Docsity.com 20