Introduction to Cool

CSC 310 - Programming Languages

Cool Overview

  • Classroom Object-Oriented Language

  • Designed to:
    • Be implementable in one semester
    • Give a taste of implementing modern features
      • Abstraction
      • Static typing
      • Inheritance
      • Dynamic dispatch
      • and more …
    • But many “gnarly” things are left out

A Simple Example

  • Cool programs are sets of class definitions
    • A special Main class with a special method main
    • Like Java
  • Class: a collection of fields and methods
  • Instances of a class are objects
  • Example

    class Point {
        x : Int <- 0;
        y : Int <- 0;
    };

Cool Objects

  • The expression “new Point” creates a new object of class Point
  • An object can be thought of as a record with a slot for each attribute

    class Point {
        x : Int <- 0;
        y : Int; (* use default value *)
    };

Cool Methods

  • A class can also define methods for manipulating its attributes
  • Methods refer to the current object using self
  • Example

    class Point {
        x : Int <- 0;
        y : Int; (* use default value *)
        movePoint(newx : Int, newy : Int) : Point {
            { x <- newx;
              y <- newy;
              self;
            } -- close block expression
        }; -- close method
    }; -- close class

Information Hiding

  • Methods are global
  • Attributes are local to a class
    • They can only be accessed by that class’s methods
  • Example:

    class Point {
        x : Int <- 0;
        y : Int <- 0;
        getx () : Int { x };
        setx (newx : Int) : Int { x <- newx };
    };

Methods and Object Layout

  • Each object knows how to access the code of its methods
  • As if the object contains a slot pointing to the code
  • In reality, implementations save space by sharing these pointers among instances of the same class

Inheritance

  • We an extend points to color points by using subclassing
  • Example:

    class ColorPoint extends Point {
        color : Int <- 0;
        movePoint(newx : Int, newy : Int) : Point {
            { color <- 0;
              x <- newx; y <- newy;
              self;
            }
        };
    };

Cool Types

  • Every class is a type
  • Base (built-in, predefined) classes:
    • Int: integers
    • Bool: booleans
    • String: for strings
    • Object: root of class hierarchy
  • All variables mus be declared
    • compiler infers types for expressions

Cool Type Checking

  • Example:

    x : Point;
    x <- new ColorPoint;
    • is well-typed if Point is an ancestor of ColorPoint in the class hierarchy

    • Rephrase: … is well typed if ColorPoint is a subtype of Point

  • Type safety: a well typed program cannot result in run-time type errors

Method Invocation and Inheritance

  • Methods are invoked by (dynamic) dispatch
  • Understanding dispatch in the presence of inheritance is a subtle aspect of object-oriented programming

  • Example:

    p: Point;
    p <- new ColorPoint;
    p.movePoint(1,2);
    • p has static type Point
    • p has dynamic type ColorPoint
    • p.movePoint must invoke ColorPoint version

Other Expressions

  • Cool is an expression language (like OCaml)
    • Every expression has a type and a value
    • Conditionals: if E the E else E fi
    • Loops: while E loop E pool
    • Case/Switch: case E of x : Type => E; ... esac
    • Assignment: x <- E
    • Primitive I/O: out_string(E), in_string(), …
    • Arithmetic, Logic Operations
  • Missing: arrays, floats, interfaces, exceptions, …

Cool Memory Management

  • Memory is allocated every time “new E” executes

  • Memory is deallocated automatically when an object is not reachable anymore
    • Done by garbage collector (GC)

Course Project

  • A complete interpreter
    • Cool course ==> Executed Program
    • No optimizations
    • Also no garbage collection, arrays, etc.
  • Split into four programming assignments