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:
@@ -0,0 +1 @@
|
||||
Bool.new() // expect runtime error: Bool metaclass does not implement 'new()'.
|
||||
@@ -0,0 +1 @@
|
||||
Class.new() // expect runtime error: Class does not implement 'new()'.
|
||||
@@ -15,6 +15,7 @@ IO.print([1, [2, [3], 4], 5].join(",")) // expect: 1,[2, [3], 4],5
|
||||
|
||||
// Calls toString on elements.
|
||||
class Foo {
|
||||
construct new() {}
|
||||
toString { "Foo.toString" }
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ IO.print([1, [2, [3], 4], 5]) // expect: [1, [2, [3], 4], 5]
|
||||
|
||||
// Calls toString on elements.
|
||||
class Foo {
|
||||
construct new() {}
|
||||
toString { "Foo.toString" }
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ IO.print({1: {2: {}}}) // expect: {1: {2: {}}}
|
||||
|
||||
// Calls toString on elements.
|
||||
class Foo {
|
||||
construct new() {}
|
||||
toString { "Foo.toString" }
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Null.new() // expect runtime error: Null metaclass does not implement 'new()'.
|
||||
@@ -0,0 +1 @@
|
||||
Num.new() // expect runtime error: Num metaclass does not implement 'new()'.
|
||||
@@ -0,0 +1 @@
|
||||
Object.new() // expect runtime error: Object metaclass does not implement 'new()'.
|
||||
@@ -1,2 +1,4 @@
|
||||
class Foo {}
|
||||
class Foo {
|
||||
construct new() {}
|
||||
}
|
||||
IO.print(!Foo.new()) // expect: false
|
||||
|
||||
@@ -24,7 +24,9 @@ IO.print(Object.same(Bool, Num)) // expect: false
|
||||
IO.print(Object.same(Bool, Bool)) // expect: true
|
||||
|
||||
// Other types compare by identity.
|
||||
class Foo {}
|
||||
class Foo {
|
||||
construct new() {}
|
||||
}
|
||||
|
||||
var foo = Foo.new()
|
||||
IO.print(Object.same(foo, foo)) // expect: true
|
||||
@@ -32,6 +34,7 @@ IO.print(Object.same(foo, Foo.new())) // expect: false
|
||||
|
||||
// Ignores == operators.
|
||||
class Bar {
|
||||
construct new() {}
|
||||
==(other) { true }
|
||||
}
|
||||
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
class Foo {}
|
||||
class Foo {
|
||||
construct new() {}
|
||||
}
|
||||
IO.print(Foo.new().toString == "instance of Foo") // expect: true
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Range.new() // expect runtime error: Range metaclass does not implement 'new()'.
|
||||
@@ -1,4 +1,6 @@
|
||||
class TestSequence is Sequence {
|
||||
construct new() {}
|
||||
|
||||
iterate(iterator) {
|
||||
if (iterator == null) return 1
|
||||
if (iterator == 10) return false
|
||||
|
||||
@@ -2,6 +2,7 @@ IO.print([].isEmpty) // expect: true
|
||||
IO.print([1].isEmpty) // expect: false
|
||||
|
||||
class InfiniteSequence is Sequence {
|
||||
construct new() {}
|
||||
iterate(iterator) { true }
|
||||
iteratorValue(iterator) { iterator }
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ class FibIterator {
|
||||
}
|
||||
|
||||
class Fib is Sequence {
|
||||
construct new() {}
|
||||
|
||||
iterate(iterator) {
|
||||
if (iterator == null) return FibIterator.new()
|
||||
iterator.iterate
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Sequence.new() // expect runtime error: Sequence metaclass does not implement 'new()'.
|
||||
@@ -1,4 +1,6 @@
|
||||
class TestSequence is Sequence {
|
||||
construct new() {}
|
||||
|
||||
iterate(iterator) {
|
||||
if (iterator == null) return 1
|
||||
if (iterator == 3) return false
|
||||
|
||||
@@ -15,6 +15,8 @@ class FibIterator {
|
||||
}
|
||||
|
||||
class Fib is Sequence {
|
||||
construct new() {}
|
||||
|
||||
iterate(iterator) {
|
||||
if (iterator == null) return FibIterator.new()
|
||||
iterator.iterate
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
String.new() // expect runtime error: String metaclass does not implement 'new()'.
|
||||
Reference in New Issue
Block a user