10 - Click to edit Master subtitle style Chapter Ten Modern...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Click to edit Master subtitle style Chapter Ten Modern Programming Languages, 2nd ed. Scope Chapter Ten Modern Programming Languages, 2nd ed. 11 Chapter Ten Modern Programming Languages, 2nd ed. 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. 22 fun square n = n * n; fun double n = n + n; fun square a = a * a; fun double b = b + b; Chapter Ten Modern Programming Languages, 2nd ed. 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. 33 Chapter Ten Modern Programming Languages, 2nd ed. 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. 44 Chapter Ten Modern Programming Languages, 2nd ed. Examples Chapter Ten Modern Programming Languages, 2nd ed. 55 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; Chapter Ten Modern Programming Languages, 2nd ed. Scope ■ There may be more than one definition for a given name ■ Each occurrence of the name (other than a definition) has to be bound according to one of its definitions ■ An occurrence of a name is in the scope of a given definition of that name whenever that definition governs the binding for that occurrence Chapter Ten Modern Programming Languages, 2nd ed. 66 Chapter Ten Modern Programming Languages, 2nd ed. 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. 77- fun square square = square * square; val square = fn : int -> int- square 3; val it = 9 : int Chapter Ten Modern Programming Languages, 2nd ed. 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. 88 Chapter Ten Modern Programming Languages, 2nd ed. Blocks ■ A block is any language construct that contains definitions, and also contains the region of the program where those definitions apply Chapter Ten Modern Programming Languages, 2nd ed....
View Full Document

This note was uploaded on 09/30/2011 for the course CS 6371 taught by Professor Hamlen during the Spring '11 term at University of Texas at Dallas, Richardson.

Page1 / 47

10 - Click to edit Master subtitle style Chapter Ten Modern...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online