feat: implement superclass constructor calls and metaclass inheritance chain

Add support for explicit superclass constructor invocation in subclass constructors using `super.constructorName(args)` syntax before the opening brace. The metaclass inheritance hierarchy now mirrors the regular class chain, with `Class` as the root metaclass superclass instead of `Object`. Includes test cases for multi-level constructor delegation and static method inheritance.
This commit is contained in:
Bob Nystrom
2013-12-17 17:39:05 +00:00
parent 93cf4e6b87
commit 656238f286
5 changed files with 99 additions and 10 deletions
+25
View File
@@ -0,0 +1,25 @@
class A {
this new(arg) {
io.write("A.new " + arg)
}
}
class B is A {
this otherName(arg1, arg2) super.new(arg2) {
io.write("B.otherName " + arg1)
}
}
class C is B {
this create super.otherName("one", "two") {
io.write("C.create")
}
}
var c = C.create
// expect: A.new two
// expect: B.otherName one
// expect: C.create
io.write(c is A) // expect: true
io.write(c is B) // expect: true
io.write(c is C) // expect: true
+3 -4
View File
@@ -2,12 +2,14 @@ class Foo {
methodOnFoo { io.write("foo") }
method(a) { io.write("foo") }
method(a, b, c) { io.write("foo") }
override { io.write("foo") }
}
class Bar is Foo {
methodOnBar { io.write("bar") }
method(a, b) { io.write("bar") }
method(a, b, c, d) { io.write("bar") }
override { io.write("bar") }
}
var bar = Bar.new
@@ -19,9 +21,6 @@ bar.method(1) // expect: foo
bar.method(1, 2) // expect: bar
bar.method(1, 2, 3) // expect: foo
bar.method(1, 2, 3, 4) // expect: bar
bar.override // expect: bar
// TODO: Overriding.
// TODO: Private fields.
// TODO: Super (or inner) calls.
// TODO: Grammar for what expressions can follow "is".
// TODO: Prevent extending built-in types.
@@ -0,0 +1,23 @@
class Foo {
static methodOnFoo { io.write("foo") }
static method(a) { io.write("foo") }
static method(a, b, c) { io.write("foo") }
static override { io.write("foo") }
}
class Bar is Foo {
static methodOnBar { io.write("bar") }
static method(a, b) { io.write("bar") }
static method(a, b, c, d) { io.write("bar") }
static override { io.write("bar") }
}
Bar.methodOnFoo // expect: foo
Bar.methodOnBar // expect: bar
// Methods with different arity do not shadow each other.
Bar.method(1) // expect: foo
Bar.method(1, 2) // expect: bar
Bar.method(1, 2, 3) // expect: foo
Bar.method(1, 2, 3, 4) // expect: bar
Bar.override // expect: bar