[Grace-core] Inheritance and object initialisation

Michael Homer mwh at ecs.vuw.ac.nz
Sun Jul 15 14:29:59 PDT 2012


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.

First, the phrase that's been used is that the expression after
"inherits" must "definitively statically resolve to an object
literal". It's not quite what the spec says at the moment but it seems
to have wide currency. I take from that that this should be valid
(annotations omitted throughout):
  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.

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?

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

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

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

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

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

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? What is tmp.foo?

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

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

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

What does a class do here?
  var tmp
  class aCreator.new {
    object {
      def x = 1
      method foo is override { "hello" }
      tmp := self
    }
  }
  def b = object {
    inherits aCreator.new
    method foo is override { "world" }
  }
Does tmp == b? What is tmp.foo?

  var tmp
  class aCreator.new {
    object {
      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?

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

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

There are probably other cases I should have included too. My
instincts for what many of these should be in isolation seem to be
contradictory with each other in general, so I'd like to check how
they're supposed to go.
-Michael


More information about the Grace-core mailing list