http flic krpLDPx N Names Binding and Scope
http: //flic. kr/p/LDPx. N Names, Binding, and Scope
Key Idea in Computing: Abstraction • Represent thing in form closer to its meaning (semantics), hiding away implementation details – Example: Separate language features from details of computer architecture • Factor out details so person can focus on a few concepts at a time • Capture only details relevant to current perspective
Names: One way to abstract • Create name to represent more complex thing • Enable focusing on “what” rather than “how” • Examples: – Variables – Functions – Classes
Notion of Binding • Mapping of name to thing it names • Examples: – Variable name bound to int data item – Function name bound to function definition – Class name bound to class definition
Notion of Binding Time • Time at which a name is bound to thing it names • Examples: – Language-design time (e. g. , primitive types) – Program-writing time (e. g. , class names) – Run time (e. g. , object references) • Two categories: – Static: Before run time – Dynamic: At run time
Design Tension: Early versus Late Binding • Early binding: – Greater efficiency (of compiler/interpreter) – Easier to comprehend • Late binding: – Greater flexibility
Binding Terminology • Key events: – – – Creation of object Creation of binding Use of binding Destruction of object • Binding lifetime: From binding creation to destruction • Dangling reference: Referent object destroyed before binding
Storage Allocation Mechanisms: Creating/Destroying Objects • Static: Objects get absolute address – Retained throughout program execution – Example: Global variable • Stack: Objects allocated/deallocated in LIFO order – Tied to subroutine calls/returns • Heap: Objects allocated/deallocated at arbitrary times – Garbage collection: Implicit/automagic deallocation
Notion of Scope • Textual region in program where binding is active • Static scope: Scope determined at compile time – Example: C Language “block scoping” • Dynamic scope: Scope determined at run time – Example: Lisp Language • Referencing environment: At any point in program execution, set of active bindings
Pascal Example: Scoping and Nested Subroutines
Language Design Issue: Declaration Order and Scoping In Java (or other C-like language of choice) • Where is define-before-use required? • Where is define-before-use not required? • Local variables in a method definition must be defined before they can be used • Class methods may be called before they are defined in a class declaration (e. g. , by another method of the class)
Language Design Issue: Modularization and Information Hiding • Info Hiding: Make objects and algorithms invisible to parts of system that don’t “need to know” • Modularization: Break system into modules with interfaces and hidden implementations – Modules may be hidden as well • In Java, give example of hidden modules being made visible and name bindings added to current scope – Hint: Think coarser granularity than a class Importing a package adds that package’s bindings to the current scope
Language Design Issue: Dynamic Scoping • Binding for given name is one encountered most recently during execution and not yet destroyed by returning from its scope • Con: Difficult to understand • Example: – If static scoping, prints 1 – If dynamic, depend on read_integer: • If pos. , prints 2 • otherwise, prints 1
Language Design Issue: Name Aliases • Multiple names map to same object • Give a Java example of an alias My. Object my. O = new My. Object(); My. Object my. Alias = my. O;
Language Design Issue: Name Overloading • One name maps to multiple objects • Give a Java example of overloading Couple examples: • Two methods in same class with different parameters • Instance variables and method parameters can have same names (shadowing)
Related but different from overloading: Coercion • Convert value of one type to value of another when second type is required by surrounding context – Typically done by compiler • C Example: double min(double x, double y) { … } … int my. X = 5, my. Y = 6; … = min(my. X, my. Y);
Related but different from overloading: Polymorphism • From the Greek: “having multiple forms” • Can apply to data structures or subroutines • Two main kinds: – Parametric polymorphism – Subtype polymorphism
Parametric Polymorphism • Code takes types as parameters – May be implicit • Ada Example:
Subtype Polymorphism • Code works with one type T, but programmer can create subtypes of T that also work • Give a Java example of subtype polymorphism public class Employee { public void print. Info() { … } public class Salaried. Employee extends Employee { public void print. Info() { … } } … Employee bob = get. Any. Type. Of. Employee(); bob. print. Info();
Polymorphic solution more general than overloaded vs
Language Design Issue: Subroutine Binding to Reference Environment • Figure 3. 14, p 152
Two Approaches to Subroutine Referencing Environments • Shallow Binding: Referencing environment not created until subroutine called • Deep Binding: Subroutine carries referencing environment with it – Bundle known as a closure • Java. Script has closures
Activity: How to implement name checking for HW 3? • Goal: Have parser give error if an identifier is undeclared • Basic pieces: – Treewalker – Symbol table – Scope stack
- Slides: 23