[Grace-core] Inheritance and object initialisation

James Noble kjx at ecs.vuw.ac.nz
Thu Jul 19 02:50:09 PDT 2012


On 18/07/2012, at 12:32 PM, Andrew Black wrote:

> This has become very long; we should move it to the Wiki.

well yes, but I've just got to this now, before the telecon tomorrow
(where I fear we may well discuss this).  So here are my answers:
note that I *haven't* looked at anyone else's answers in any depth!

I've listed Michael's old examples, Michael's new examples,
and then Andrew's examples at the end 

That way, when my answers are inconsistent or just plain wrong,
I'll claim it's the first week of term and leave it at that!

>  def a = object {
>    def x = 1
>  }
>  def b = object {
>    inherits a
>    def y = 2
>  }
> That is required by the "inherits defaultObject" part of the recent
> notes as well, so I assume it works.

seems fine to me!

> Building on the above, I should also be able to write this:
>  def a = object {
>    def x = object { var t := 2 }
>  }
>  def b = object {
>    inherits a
>  }
> Does a.x == b.x?

yes

>  def a = object {
>    def x = { self }
>  }
>  def b = object {
>    inherits a
>    def y = 2
>  }
> Does a.x == b.x?

no

> Does a.x.apply == b.x.apply?

no

> How about this?
>  var tmp := 1
>  def a = object {
>    def x = tmp
>  }
>  tmp := 2
>  def b = object {
>    inherits a
>  }
> Does a.x == b.x?

yes

> What is b.x?

1

>  var tmp := 1
>  def a = object {
>    def x = tmp * 2
>    tmp := tmp + 1
>  }
>  def b = object {
>    inherits a
>  }
> Does a.x == b.x?

yes 

> What is b.x? 

1    
(kjx later oops - clearly that's impossible. It's 2)

> What is tmp?

2

>  var tmp
>  def a = object {
>    def x = 1
>    method foo { "hello" }
>    tmp := self
>  }
>  def b = object {
>    inherits a
>    method foo is override { "world" }
>  }
> Does tmp == a?

yes

> Does tmp == b?

no

> What is tmp.foo?

"hello"

> This should work:
>  method aCreator {
>    object {
>      def x = 1
>    }
>  }
>  def b = object {
>    inherits aCreator
>    def y = 2
>  }


sure 

> More complicatedly:
>  var tmp
>  method aCreator {
>    object {
>      def x = 1
>      method foo { "hello" }
>      tmp := self
>    }
>  }
>  def b = object {
>    inherits aCreator
>    method foo is override { "world" }
>  }
> Does tmp == b?

oh this one is good!
I'm going to say "unspecified" - which is bad!
and then say "yes" which is worse :-)

> What is tmp.foo?

unspecified or "world"

> Or:
>  var tmp
>  method aCreator {
>    object {
>      def x = 1
>      method foo { "hello" }
>      tmp := { foo }
>    }
>  }
>  def b = object {
>    inherits aCreator
>      method foo { "world" } 
>  }
> What is tmp.apply?

unspecified; "world".

>  var tmp := 0
>  method aCreator {
>    object {
>      def x = 1
>      tmp := tmp + 1
>    }
>  }
>  def a = aCreator
>  def b = object {
>    inherits a
>  }
> What is tmp?

1
how could it be otherwise?

> 
>  var tmp := 0
>  method aCreator {
>    def ret = object {
>      def x = 1
>      tmp := tmp + 1
>    }
>    ret
>  }
>  def b = object {
>    inherits aCreator
>  }
> What is tmp?

1

> What does a class do here?

can be understood by desugaring

>  var tmp
>  class aCreator.new { // kjx removed unintended object
>      def x = 1
>      method foo is override { "hello" }
>      tmp := self
>  }
>  def b = object {
>    inherits aCreator.new
>    method foo is override { "world" }
>  }

technically this is an error: aCreator.foo does not override anything.

> Does tmp == b?

underspecified - yes .

> What is tmp.foo?

"world"

>  var tmp
>  class aCreator.new {
>      def x = 1
>      method foo is override { "hello" }
>      tmp := { foo }
>    }
>  }
>  def b = object {
>    inherits aCreator.new
>    method foo is override { "world" }
>  }
> What is tmp.apply?

unspecified - I like "world"

>  class aCreator.new {
>      def x = 1
>      method foo is { "hello" }
>      def y = { foo }
>    }
>  }
>  def b = object {
>    inherits aCreator.new
>    method foo is override { "world" }
>  }
> What is b.y.apply?

I like "world"
The more I think about it, it's really clear this needs to be world

>  var tmp
>  class aCreator.new {
>    def x = { self }
>    tmp := x
>  }
>  def b = object {
>    inherits a
>    def y = 2
>  }
> Does tmp == b.x?

yes

> Does tmp.apply == b.x.apply?

yes  follows from the above

> 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?

2

>  def a = object {
>    def x = object {
>      inherits a
>      def y = 1
>    }
>  }

I'd be quite tempted to outlaw that - we haven't talked about it, 
but my head exploded and there is grey goo dribbling out my ears.

> Does a.x == a.x.x?

^C

(Ok, actually the answer is yes, and I typed this before looking at the other answers)

>  method aCreator(blk) {
>      method y { 1 }
>      def x = blk.apply
>    }
>  }
>  def b = object {
>    inherits aCreator { self.y }
>    method y { 2 }
>  }

I'm half-tempted to say this should also be a compile-time error 
because I have weird ideas about where the code to the right of an "inherits" should be executed,
and where I want it executed doesn't have a "y" method.


> What is b.x?

2 

(Andrew's extra examples) 

> 
> def a =
> 	{	var tmp := 1
> 		def result =  object {
>   				def x = tmp
>         	}
> 		tmp := tmp + 1
> 	}.apply
> 
> def b = object {
>         inherits a
> }

as written, a is "done".  Can we inherit from done?  
If not, - if we add make the last line "tmp := tmp + 1; result"
Then a gets set to an object with a single "x" def. 
a inherits from b without reevaluating the outer block;
because b has no additional methods or anything, b == a 

OK, now I'll try to look over the other answers.....    long wait... 
so it's pretty clear my interpretation differs from everyone else's! 

I wonder if anyone can back-form
 a) my semantics for "inherits"
and
 b) why I picked that semantics

James





More information about the Grace-core mailing list