[Grace-core] Inheritance and Template Objects

James Noble kjx at ecs.vuw.ac.nz
Fri Apr 5 19:35:39 PDT 2013


On 6/04/2013, at 14:02 PM, Andrew P. Black wrote:

> It's a Bad Idea (capital letters) to have operations like x()y() answering both templates and instances.  

so I think that's a crucial questions - are we willing to have operations / factory methods that return instances when the operations are called from normal code, and return "templates" / part objects when the operations are called from inherits clauses or object constructors?

> Explicit use templates would not be necessary, because you could always get a template out of a prototype object (using the "soul sucking" operation

the "soul sucking" operation makes templates explicit - but I don't think the template proposal depends on "soul sucking", because you can always declare all the templates you want explicitly. 

As I understand it, "soul sucking" is what lets you "inherit" from pre-existing objects in the templates system.  The rest of the template system will work without "soul sucking".
This is pretty much parallel to the way the factory method system could let you inherit from a clone.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

That comes back to how I understood classes in the template system would have to work.

The class

class C.m(mm) {
 body
}

would expand to

def C = object {
  method m(mm) {return **template** body}
}

to instantiate an instance you'd write  "object C.m(mm)"   (module syntax: s/object/new/) 

This encoding lets you write a subclass in the natural way, with the expected semantics:

class D.n(nn) { 
  inherits C.m(nn) {
}

which you again instantiate by writing "object D.n(nn)"

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

The problem with classes returning objects - rather than object templates - 
is that you can't then inherit from those classes without "soul sucking". GIven:

def C' = object {
  method m(mm) {return object body}
}

to subclass, you'd have to inherit from the template:

class D'.n(nn) { 
  inherits C'.m(nn).template {
}

thus making the C' object. initializing it, sucking its soul then throwing it on the floor,
then starting again from the soul to make the D' object, redoing all the initialization etc.
If we want to inherit from classes, then it seems to me classes need to return templates not objects.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

At that point - before we start thinking about MI etc - the only practical difference between the template system and the classical system (that is currently in the specification)  is that the template system means you have to write  "object C.m(mm)"  (analogous to "new C.m(mm)"  to instantiate that class, whereas in the classical system you just write "C.m(mm)".    On the other hand, the classical system has issues about the interpretation of those class expressions.   

I note, somewhat ironically, that the issues seem to arise only in a dynamically typed language.
Statically typed languages have no problem resolving semantics based on syntatical or type contexts (exhibit #1: Haskell, exhibit #2, Scala) --- in the classical system is is always clear syntactically whether you're in an object or class context.  Alternatively, you could have a class system where you had to write "new" or "object" to call constructors, and it will all still work.

James 





More information about the Grace-core mailing list