12 Written: Objects

These are due by midnight on Thursday, April 16.

Object Field Layout

On each object, Java has storage for fields that come from each level of the object’s class hierarchy. It’s natural to ask if this is true for objects in other languages.

Choose a language other than Java that supports classes and inheritance, and illustrate whether this is true for that language with a short program.


With

JavaScript has a version of with.

This file contains a simple interpreter extended with anonymous objects that support field access (e-get). It also has a new operation called with.

  1. Describe the behavior of with in English.

  2. Consider what it would take to extend unbound-ids from the scope written assignment in order to accommodate these new constructs, and describe any interesting problems that would come up. A precise test case or two could go a long way here.

  3. How would your answer to (2) change if the language had a simple type system with object types that indicated the names and types of fields statically (e.g. this means that the type of the object passed to with can be known by unbound-ids)?


Recursive Types

There is a section in PAPL on this issue that we didn’t tackle in class.

  1. What problem arises when trying to give a simple record type to o in this program? What recursive type could you give it (using the μ-notation from the book)?

    rec o = { m: lam(x): o end } o.m(0)

  2. In the Javette assignment it’s possible to write something analogous that successfully type-checks:

    (program (classes (class C extends Object (fields) (methods (method C m (Num x) this)))) (call (new C) m 0))

    What about Javette’s type system avoids needing the recursive type in (1)?