[Grace-core] Class Encoding

Andrew P. Black black at cs.pdx.edu
Wed Jun 22 14:17:02 PDT 2011


Dear Eric,

The meaning of classes — I prefer this term to "encoding", although I know that Prof Kim Bruce feels differently — on the Wiki and in the spec also allows for classes to be implemented in the language.  At least, if it doesn't, then I think that we need to fix that!  

Your encoding is much more complicated, because it separates the creation of the objet from its initialization (as does Smalltalk and Java).  First, you need several hidden methods (I presume that's what the method names with underscores are; underscore is not currently a permitted identifier character).  Moreover, if you were to change your example to use const fields rather than var fields, then the separation becomes impossible.  That was one of the forces that pushed us towards this pattern of nested object constructors that get their initializing values from the surrounding scope.

With the semantics from the Wiki, 
> 
> The following code:
> 
> class a {(n:Number) ->
>    var x:Number := n
> }

Note that there are no parenthesis necessary or permitted abound the lambda-bound variables.
> 
> class b {(m:Number) ->
>    extends a.new(m+1)
>    var y:Number := m
> }
> 
> would be rewritten as:

const a := object {
   method new(n:Number) -> type {x->Number, x:=(Number)} {
       //returns fully initialized object
       object objA {     // The spec does not currently allow objects to have local names; they are all called self.
			      //  I agree with you that this would be useful, if only for purposes of discussion 
			      // (so we can say "objA has one var field") but it's one more "option"
           var x:Number := n
       }
   }
}

const b := object {
   method new(m:Number) -> type {x->Number, x:=(Number), y->Number, y:=(Number)} {  
       object objB {
	   var y:Number := m
	} extends a.new(m+1)
   }
}
   

This allows for classes to be implemented within the language, while
still allowing concatenation (as opposed to delegation) as the method
for inheritance (http://portal.acm.org/citation.cfm?id=219264, cited
by Andrew Black on the wiki).

This is a lot shorter than your encoding, seems simpler, needs no hidden methods, and works for const fields.

	Andrew



More information about the Grace-core mailing list