[Grace-core] Multiple inheritance & module inheritance
Michael Homer
mwh at ecs.vuw.ac.nz
Mon Mar 9 00:26:55 PDT 2015
These are the examples motivating positional inheritance, also
available from <http://ecs.vuw.ac.nz/~mwh/positional-motivation.txt>.
Here is the problematic case with what Kim does now:
class widget.new {
makeWindow(self.width)
}
class button.new {
inherits widget.new
var width := 100
}
(uninitialised variable error: width is used before set)
In reality, this may be a method using a field, rather than
the field itself, but to keep the example simple I have
left that out.
With position-dependent inheritance, we can resolve that so
downcalls are safe:
class widget.new {
makeWindow(self.width)
}
class button.new {
var width := 100
inherits widget.new
}
(Works! width is now set before makeWindow(self.width) runs)
What if the style we're using requires upcalls, rather than
downcalls? That will be a problem with inheritance at the
end:
class widget.new {
var textColour := "#000"
method getColour(for) {
if (for == "text") then {
return textColour
} else {
return "#fff"
}
}
}
class button.new {
var colour := getColour "text"
inherits widget.new
}
(Error! Same as the first example.)
But we can fix them by putting the inherits clause at the
top again:
class widget.new {
method getColour(for) { ... }
}
class button.new {
inherits widget.new
var colour := getColour "text"
}
(Works! The superclass initialisation has run before use.)
What if we want both? We can order the operations so that
everything works out:
class widget.new {
makeWindow(self.width)
method getColour(for) { ... }
}
class button.new {
var width := 100
inherits widget.new
var colour := getColour "text"
}
(Works! Required subclass initialisation runs before
inheritance that uses it, which itself runs before subclass
code that relies on superclass inheritance.)
More information about the Grace-core
mailing list