More Type Checking

Assignment

Initialized Attributes

Method Dispatch

Notes on Dispatch

An Extended Typing Judgment

The Method Environment

The Dispatch Rule Revisited

Static Dispatch

Static Dispatch (Continued)

\[ \frac{ \begin{array}{l} O, M \vdash e_0 : T_0\\ O, M \vdash e_1 : T_1\\ ...\\ O, M \vdash e_n : T_n\\ T_0 \leq T\\ M(T, f) = (T_1', \ldots, T_n', T_{n+1}')\\ T_i \leq T_i' \; \text{for} \; 1 \leq i \leq n \end{array}} {O, M \vdash e_0@T.f(e_1, \ldots, e_n) : \; T_{n+1}'}\text{[Static Dispatch]} \]

Flexibility vs. Soundness

Dynamic and Static Types

Recall: Soundness

An Example

Continuing Example

Post-Mortem

SELF_TYPE to the Rescue

SELF_TYPE to the Rescue (Continued)

SELF_TYPE as a Tool

SELF_TYPE and Dynamic Types

SELF_TYPE and Dynamic Types (Continued)

Type Checking

Operations on SELF_TYPE

Extending \(\leq\)

Let \(T\) and \(T'\) be any types except SELF_TYPE. There are four cases in the definition of \(\leq\)

  1. \(\texttt{SELF_TYPE}_C \leq T\) if \(C \leq T\)

    • \(\texttt{SELF_TYPE}_C\) can be any subtype of \(C\)
    • This includes \(C\) itself
    • Thus this is the most flexible rule we can allow
  2. \(\texttt{SELF_TYPE}_C \leq \texttt{SELF_TYPE}_C\)

    • \(\texttt{SELF_TYPE}_C\) is the type of the self expression
    • In Cool, we never need to compare SELF_TYPEs comming from different classes

Extending \(\leq\) (Continued)

  1. \(T \leq \texttt{SELF_TYPE}_C\) is always false

    • Note: \(\texttt{SELF_TYPE}_C\) can denote any subtype of \(C\)
  2. \(T \leq T'\) (according to the rules from before

Based on these rules, we can extend \(lub\)

Extending \(lub(T, T')\)

Let \(T\) and \(T'\) be any types except SELF_TYPE. Again, there are four cases:

  1. \(lub(\texttt{SELF_TYPE}_C, \texttt{SELF_TYPE}_C) = \texttt{SELF_TYPE}_C\)
  2. \(lub(\texttt{SELF_TYPE}_C, T) = lub(C, T)\)
  3. \(lub(T, \texttt{SELF_TYPE}_C) = lub(T, C)\)
  4. \(lub(T, T')\) defined as before

Where Can SELF_TYPE Appear in Cool?

Where Can SELF_TYPE Appear in Cool?

Typing Rules for SELF_TYPE

Type Checking Rules

What is Different?

The Big Rule for SELF_TYPE

What is Different?

Static Dispatch

Static Dispatch

Static Dispatch

New Rules

Where is SELF_TYPE Illegal in Cool?

Summary of SELF_TYPE

Why Cover SELF_TYPE?

Type Systems