Understanding Scope and Namespaces in Modern Programming Languages, Slides of Advanced Computer Programming

An in-depth exploration of the concept of scope and namespaces in modern programming languages. It covers various aspects such as block scope, separate compilation, static and dynamic scoping, and namespaces. The document also includes examples and comparisons between different programming languages like ml, java, and fortran.

Typology: Slides

2012/2013

Uploaded on 04/18/2013

palvani
palvani 🇮🇳

4.5

(2)

83 documents

1 / 47

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Scope
Chapter Ten Modern Programming Languages, 2nd ed. 1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f

Partial preview of the text

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