Download presentation
Presentation is loading. Please wait.
1
Names, Binding, and Scope
Names, Binding, and Scope
2
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
3
Names: One way to abstract
Create name to represent more complex thing Enable focusing on “what” rather than “how” Examples: Variables Functions Classes
4
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
5
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
6
Design Tension: Early versus Late Binding
Early binding: Greater efficiency (of compiler/interpreter) Easier to comprehend Late binding: Greater flexibility
7
Binding Terminology Key events:
Creation of object Creation of binding Use of binding Destruction of binding Destruction of object Binding lifetime: From binding creation to destruction Dangling reference: Referent object destroyed before binding
8
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
9
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
10
Pascal Example: Scoping and Nested Subroutines
11
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)
12
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
13
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
14
Language Design Issue: Name Aliases
Multiple names map to same object Give a Java example of an alias MyObject myO = new MyObject(); MyObject myAlias = myO;
15
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)
16
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 myX = 5, myY = 6; … = min(myX, myY);
17
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
18
Parametric Polymorphism
Code takes types as parameters May be implicit Ada Example:
19
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 printInfo() { … } … } public class SalariedEmployee extends Employee { Employee bob = getAnyTypeOfEmployee(); bob.printInfo();
20
Polymorphic solution more general than overloaded
vs
21
Language Design Issue: Subroutine Binding to Reference Environment
Figure 3.14, p152 One might argue that deep binding is appropriate for the environment of function older_than_threshold (for access to threshold), while shallow binding is appropriate for the environment of procedure print_person (for access to line_length).
22
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 JavaScript has closures
23
Activity: How to implement name checking for HW3?
Goal: Have parser give error if an identifier is undeclared Basic pieces: Treewalker Symbol table Scope stack
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.