Type Checking
Outline
2
Expressions and Statements
3
Side Effects of Expression Evaluation
x = 5; y = 1 + x++; if (y == x) printf("OK");
x = 5; y = 1 + 5; if (y == x) printf("OK");
4
Order of Evaluation
5
Defined Order of Evaluation in C
6
Scopes
Global variables
Local variables
Dynamically allocated memory
7
Types
8
Examples of Type Checking
9
Static Typing
10
Dynamic Typing
11
Type (and memory) safety
12
Examples of Types
13
Numeric Types
14
Enumeration Types
enum hue { chartreuse, burgundy, claret=20, winedark };
/* the set of integer constant values is { 0, 1, 20, 21 } */
enum hue col, *cp;
col = claret; cp = &col;
if (*cp != burgundy) …
enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
15
Types as Sets of Values
16
Monomorphism vs. Polymorphism
17
Types of Polymorphism
parametric
universal
inclusion (subset)
polymorphism
overloading
ad hoc
coercion
18
Types of Polymorphism
parametric
universal
inclusion (subset)
polymorphism
overloading
ad hoc
coercion
19
Coercion
20
Coercions
21
Types of Polymorphism
parametric
universal
inclusion (subset)
polymorphism
overloading
ad hoc
coercion
22
(operators or functions)
Types of Polymorphism
parametric
universal
inclusion (subset)
polymorphism
overloading
ad hoc
coercion
23
Parametric Polymorphism: Generics in Java
package java.util;
public interface Set<E> extends Collection<E> { …
Iterator<E> iterator();
boolean add(E e);
boolean addAll(Collection<? extends E> c); }
class Rectangle { … }
class SwissRectangle extends Rectangle { … }
Set<Rectangle> s = new HashSet<Rectangle>();
s.add(new Rectangle(1.,2.)); s.add(new SwissRectangle(3.,4.,5));
Set<SwissRectangle> s2 = new TreeSet<SwissRectangle>();
s2.add(new SwissRectangle(6.,7.,8)); s.addAll(s2);
24
Types of Polymorphism
parametric
universal
inclusion (subset)
polymorphism
overloading
ad hoc
coercion
25
Inclusion (Subset) Polymorphism
26