Use more conventional syntax for constructors.

They are now invoked like "new Foo".
Also, superclass constructors are now much less semantically
and syntactically weird. Since the instance is created before
any constructor is called, there's no point in time where the
instance isn't there.
This commit is contained in:
Bob Nystrom
2013-12-19 07:02:27 -08:00
parent b880b17db9
commit 4d67f2270a
30 changed files with 171 additions and 190 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ class Foo {
}
// A class with no constructors gets an argument-less "new" one by default.
var foo = Foo.new
var foo = new Foo
io.write(foo is Foo) // expect: true
io.write(foo.toString) // expect: Foo
-20
View File
@@ -1,20 +0,0 @@
class Foo {
this bar { io.write("this bar") }
this baz { io.write("this baz") }
this bar(arg) { io.write("this bar " + arg) }
toString { return "Foo" }
}
// Different names.
Foo.bar // expect: this bar
Foo.baz // expect: this baz
// Can overload by arity.
Foo.bar // expect: this bar
Foo.bar("one") // expect: this bar one
// Returns the new instance.
var foo = Foo.bar // expect: this bar
io.write(foo is Foo) // expect: true
io.write(foo.toString) // expect: Foo
+7 -8
View File
@@ -1,18 +1,17 @@
class Foo {
// TODO: Do we want to require an explicit "new" here?
this new { io.write("zero") }
this new(a) { io.write(a) }
this new(a, b) { io.write(a + b) }
new { io.write("zero") }
new(a) { io.write(a) }
new(a, b) { io.write(a + b) }
toString { return "Foo" }
}
// Can overload by arity.
Foo.new // expect: zero
Foo.new("one") // expect: one
Foo.new("one", "two") // expect: onetwo
new Foo // expect: zero
new Foo("one") // expect: one
new Foo("one", "two") // expect: onetwo
// Returns the new instance.
var foo = Foo.new // expect: zero
var foo = new Foo // expect: zero
io.write(foo is Foo) // expect: true
io.write(foo.toString) // expect: Foo
+12 -10
View File
@@ -1,6 +1,6 @@
class A {
this new(arg) {
io.write("A.new " + arg)
new(arg) {
io.write("new A " + arg)
_field = arg
}
@@ -8,8 +8,9 @@ class A {
}
class B is A {
this otherName(arg1, arg2) super.new(arg2) {
io.write("B.otherName " + arg1)
new(arg1, arg2) {
super(arg2)
io.write("new B " + arg1)
_field = arg1
}
@@ -17,18 +18,19 @@ class B is A {
}
class C is B {
this create super.otherName("one", "two") {
io.write("C.create")
new {
super("one", "two")
io.write("new C")
_field = "c"
}
cField { return _field }
}
var c = C.create
// expect: A.new two
// expect: B.otherName one
// expect: C.create
var c = new C
// expect: new A two
// expect: new B one
// expect: new C
io.write(c is A) // expect: true
io.write(c is B) // expect: true
io.write(c is C) // expect: true