[Grace-core] Permitting multiple constructors and inheritance for classes

Michael Homer mwh at ecs.vuw.ac.nz
Thu Feb 13 13:51:04 PST 2014


Hi,
Here is a proposal for doing that. Again, there is a one-sentence
version at the end. Teaser: What if we could put more than a single
method into a class object within the current syntax and semantics,
and with all current code remaining valid?

The class syntax of Grace is a syntactic sugar for a pair of nested
objects and a method.
  class foo.bar(val) {
    var x := val
    method do { ... }
  }
is translated directly to
  def foo = object {
    method bar(val) {
      object {
        var x := val
        method do { ... }
      }
    }
  }
The class object has only the one method in it, returning the instance
object. It is not possible to have multiple constructors on a class,
to have the class object itself inherit from anything, or to have
static or class methods.

I am proposing an extension to the current class syntax: inside a
class body, top-level keywords may be prefixed with "class" to place
the construct inside the outer ("class") object, rather than in the
inner ("instance") object. This would apply at least to the "method"
and "inherits" keywords, but "var" and "def" seem reasonable too, and
"class" and "type" are at least possibilities.

This may be made clearer with an example:
  class foo.bar(val) {
    class inherits baz

    var x := val
    method do { ... }

    class def valueOfZero = 0

    class method zero {
      foo.bar(valueOfZero)
    }
  }
would be translated to:
  def foo = object {
    inherits baz
    def valueOfZero = 0
    method zero {
      foo.bar(valueOfZero)
    }

    method bar(val) {
      object {
        var x := val
        method do { ... }
      }
    }
  }

With this syntax, a class object could both have additional methods of
its own and inherit methods from elsewhere (perhaps standard
constructors for a family of collections, for example). The
syntactic-sugar object-unrolling story for classes remains true, and
the semantics are still those of ordinary objects.

This "double keyword" syntax has no ambiguity and doesn't conflict
with any existing code; keywords are not valid variable or method
names, so "class inherits" was never valid code and was always
previously a syntax error. It is also clear in reading what's
happening: you are defining a class method with "class method"
(principle: keywords name what they declare!). It's not a likely
combination of tokens to arise accidentally.

Outside of a class body, or anywhere other than the top level inside
one, the syntax would remain a syntax error, since it doesn't make
sense without the syntactic sugar transformation.

All existing valid code would continue to behave exactly as it does
now. The only difference is the ability to add methods and inherits
clauses to the class object itself.

Short version
=============
Write "class method x {}" or "class inherits bar" inside a class body
to put methods or inherits clauses into the class object rather than
the instance.
-Michael


More information about the Grace-core mailing list