[Grace-core] Inheritance and object initialisation

Kim Bruce kim at cs.pomona.edu
Tue Jul 17 10:56:59 PDT 2012


I'm a little surprised that no one else has weighed in on Michael's questions and my responses.  I'm about to sit down and write down a sketch of the formal semantics of classes/objects if no one has any objections to my interpretation.  On the other hand, it's a fairly tedious task and I don't want to do it if anyone objects strongly to my interpretation.  What say you?

Kim



On Jul 16, 2012, at 1:52 PM, Kim Bruce wrote:

> I think it's important to give the answers to these along principled lines rather than an ad hoc way.  I can give a complete formal semantics for classes and objects (which will likely be more accurate than my working through these examples one by one), if everyone agrees with my interpretation of objects as instantiations of anonymous classes and that these anonymous classes are the ones that are actually inherited from.
> 
> One semi-independent decision is the interpretation of "self" in blocks/closures.  I propose that both "self" and "return" should be evaluated as they would be in the containing environment, rather than in the object encoding the block/closure.  Thus in
> 
> class C.new {
>     def x = 1
>     def y = {self.x}
> }
> 
> the interpretation of self is the object generated by C rather than being interpreted as the closure itself (which is of course encoded as an object).
> 
> Aside from that, I believe all the questions as stated for classes only are pretty straightforward as the evaluations are delayed until the application of the constructor (which calls the constructor of the superclass).
> 
> Given the ambiguities that Michael has illustrated, I don't know if we want to reconsider allowing inheritance from objects (though I understand that Andrew feels strongly about this).  I wish I knew more why Scala prohibits inheritance from objects!
> 
> I intend to play around a bit more with understanding inheritance from the viewpoint of interpreting classes as abbreviations of nested objects (as we have been to this point).  That always made me nervous from a types point of view -- and I am now coming back to my original intuition that type difficulties often reflect hidden semantic complexities, which are now coming to light.
> 
> Thanks to Michael for recognizing and exploring these issues!
> 
> Kim
> 
> 
> 
> On Jul 16, 2012, at 3:36 AM, Michael Homer wrote:
> 
>> On Mon, Jul 16, 2012 at 9:29 AM, Michael Homer <mwh at ecs.vuw.ac.nz> wrote:
>>> Hi,
>>> I've been trying to deal to a bug Kim reported a while back and maybe
>>> some others, but in doing so I've realised I don't understand what the
>>> semantics of inheritance are supposed to be. My instincts for
>>> different cases aren't consistent with each other. I'm going to give
>>> some examples, and then generally questions on what particular
>>> expressions evaluate to afterwards to clarify.
>> Here are a few more examples that I forgot:
>> def a = object {
>>   var x := 1
>> }
>> a.x := 2
>> def b = object {
>>   inherits a
>>   def y = 2
>> }
>> What is b.x?
> 
> 1
>> 
>> def a = object {
>>   def x = object {
>>     inherits a
>>     def y = 1
>>   }
>> }
>> Does a.x == a.x.x?
> 
> This one makes my head hurt!
>> 
>> method aCreator(blk) {
>>   object {
>>     method y { 1 }
>>     def x = blk.apply
>>   }
>> }
>> def b = object {
>>   inherits aCreator { self.y }
>>   method y { 2 }
>> }
>> What is b.x?
> 
> 2 (though it might also be an initialization order error)
> 
>> -Michael
>> _______________________________________________
>> Grace-core mailing list
>> Grace-core at cecs.pdx.edu
>> https://mailhost.cecs.pdx.edu/mailman/listinfo/grace-core
> 
> _______________________________________________
> Grace-core mailing list
> Grace-core at cecs.pdx.edu
> https://mailhost.cecs.pdx.edu/mailman/listinfo/grace-core



More information about the Grace-core mailing list