fix: remove default constructors and require explicit construct new() for all user classes

The compiler no longer auto-generates a default `new()` constructor for classes. All classes must now explicitly define `construct new() {}` to be instantiable. This change removes the `createDefaultConstructor` function from the compiler, eliminates the `return_this` primitive used by Object's default initializer, and adds explicit constructors to all test and example classes. Core metaclasses (Bool, Class, Null, Num, Object, Range, Sequence) now correctly reject `new()` calls with runtime errors.
This commit is contained in:
Bob Nystrom
2015-09-01 15:16:04 +00:00
parent 4665ec1913
commit ef93496fd4
86 changed files with 248 additions and 133 deletions
@@ -5,6 +5,8 @@ class Base {
}
class Derived is Base {
construct new() {}
foo(a) {
IO.print("Derived.bar(a)")
super
@@ -5,6 +5,8 @@ class Base {
}
class Derived is Base {
construct new() {}
bar {
IO.print("Derived.bar")
super.foo
@@ -5,6 +5,8 @@ class Base {
}
class Derived is Base {
construct new() {}
foo {
IO.print("Derived.foo")
super.foo
+1
View File
@@ -3,6 +3,7 @@ class Base {
}
class Derived is Base {
construct new() {}
getClosure { Fn.new { super.toString } }
toString { "Derived" }
}
+2
View File
@@ -5,6 +5,8 @@ class Base {
}
class Derived is Base {
construct new() {}
foo {
IO.print("Derived.foo")
super
@@ -7,6 +7,8 @@ class A {
class B is A {}
class C is B {
construct new() {}
foo {
IO.print("C.foo")
super.foo
@@ -1,6 +1,7 @@
class Base {}
class Derived is Base {
construct new() {}
foo { super.doesNotExist(1) } // expect runtime error: Base does not implement 'doesNotExist(_)'.
}
@@ -6,6 +6,8 @@ class A {
toString { "A.toString" }
}
class B is A {}
class B is A {
construct new() {}
}
IO.print(B.new().callSuperToString) // expect: instance of B
@@ -4,6 +4,8 @@ class A {
toString { "A.toString" }
}
class B is A {}
class B is A {
construct new() {}
}
IO.print(B.new().callSuperToString) // expect: instance of B