[Grace-core] Considering Traits as Objects in Grace
James Noble
kjx at ecs.vuw.ac.nz
Wed Mar 6 14:48:49 PST 2013
On 7/03/2013, at 11:33 AM, James Noble wrote:
> * news - I found a list of these patterns in my notebook from 2012. Here's the list!
oops: here's the list...
- a singleton object
- a repeatedly instantiable object
- a repeatedly instantiable object with per-instance def fields
- a repeatedly instantiable object with object registration
- a repeatedly instantiable object with block self bindings
- extending a pre-existing definition, adding extra methods and fields (keeping existing fields)
- extending a pre-existing definition with object registration / block self bindings
- extending a pre-existing defintion but with initialisation-time upcalls/downcalls that must resolve
the same way they would in the finished object
- extending multiple pre-existing definitions
- extending multiple pre-existing definitions and call-site choosing *specific* overridden methods
- extending only some parts of a pre-existing definition
- multiple "constructors" for a class
- "Class side" state - count number of instances etc
- inheriting constructors into a subclass?
- create an instance that satisfies an interface (Dart style)
- mutually referring defs (Marco placeholder stylee)
And here's one sketch of a design for each of these.
The semantics are simple and brutal: inheritance only from classes, self may not be used in constructors
- a singleton object
def x = object { ... }
- a repeatedly instantiable object
class C.new { ... }
- a repeatedly instantiable object with per-instance def fields
class C.new(x,y) { def f = x ... }
- a repeatedly instantiable object with object registration
can't be done easily. work around cannot then be inherited from.
def C = object {
class C' { ... }
method new {
def inst = C'.new { ... }
register(inst)
return inst }
}
- a repeatedly instantiable object with block self bindings
class C.new { ... ... {some block} ... }
- extending a pre-existing definition, adding extra methods and fields (keeping existing fields)
class D.new {
extends C.new ...
}
- extending a pre-existing definition with object registration / block self bindings
blocks OK, extension after registration can't easily be done.
- extending a pre-existing defintion but with initialisation-time upcalls/downcalls that must resolve
the same way they would in the finished object
not permitted
- extending multiple pre-existing definitions
not permitted
- extending multiple pre-existing definitions and call-site choosing *specific* overridden methods
not permitted
- extending only some parts of a pre-existing definition
not permitted
- multiple "constructors" for a class
not permitted - can be coded up but then cant be extended:
def C = object {
class C' { ... }
method new {
def inst = C'.new { ... }
register(inst)
return inst }
method otherNew { return C'.new(X) }
}
well C' can be extended, but not C as it stands.
- "Class side" state - count number of instances etc
can be coded up as above, but then external wrapper cannot be extended
- inheriting constructors into a subclass?
can be coded up as above, but then external wrapper cannot be extended
- create an instance that satisfies an interface (Dart style)
can't be done
- mutually referring defs (Marco placeholder stylee)
can't be done
More information about the Grace-core
mailing list