[Grace-core] Encoding classes

Kim Bruce kim at cs.pomona.edu
Thu Jul 14 17:33:39 PDT 2011


I continue to worry about whether we have a type-safe encoding of classes as objects with new methods.  Has anyone worked out the details?

A particular concern is objects with non-public methods (esp those accessible to subclasses, but not the general public).  Let's call those methods "private" for now, even thought they are closer to "protected" in Java and C++.

For example, suppose we have type Pair

type Pair {mkDiffPt(dx:Number,dy:Number) -> Pair}

where mkDiffPt creates a new pair that differs from the old by dx and dy.

A class PairClass generating objects should then have a type like:

type PairClassType {new: (x:Number,y:Number) -> Pair}

However, we notice that all classes generating Pairs that have a constructor taking a pair of numbers have exactly the same type.  This may be fine, but is going to cause difficulty if our class has non-public methods.  For example, suppose PairClass has a private method "helper".

The problem arises when I want to override "helper":

class ExtPairClass {x, y -> extends PairClass.new(x,y)
                                     <override> method helper(...) -> ... {...}
}

As I see it, the type system has no way of knowing whether PairClass has a helper method or not and hence whether or not the override is legal.  In particular, if I replace all occurrences of PairClass in the above by another class that also has type PairClassType (but no helper method), it should type check with exactly the same result.

My point is that Classes are more than constructors and the public methods.  In my languages, the types of classes including all protected features as well as those that were public.  I'm not at all sure how that can be done with this encoding.


Kim





More information about the Grace-core mailing list