Named, user-defined constructors.

This commit is contained in:
Bob Nystrom
2013-11-20 07:20:16 -08:00
parent 32144dae20
commit 3b0e962a05
5 changed files with 150 additions and 68 deletions
+20
View File
@@ -0,0 +1,20 @@
class Foo {
this bar { io.write("this bar") }
this baz { io.write("this baz") }
this bar(arg) { io.write("this bar " + arg) }
toString { "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