Make constructors just methods.

* Eliminate "new" reserved word.
* Allow "this" before a method definition to define a constructor.
* Only create a default constructor for classes that don't define one.
This commit is contained in:
Bob Nystrom
2015-07-10 09:18:22 -07:00
parent 0ddaa2517c
commit 5fb6186d7d
221 changed files with 864 additions and 654 deletions
+6 -6
View File
@@ -1,13 +1,13 @@
// Ported from the Python version.
class Tree {
new(item, depth) {
this new(item, depth) {
_item = item
if (depth > 0) {
var item2 = item + item
depth = depth - 1
_left = new Tree(item2 - 1, depth)
_right = new Tree(item2, depth)
_left = Tree.new(item2 - 1, depth)
_right = Tree.new(item2, depth)
}
}
@@ -27,9 +27,9 @@ var stretchDepth = maxDepth + 1
var start = IO.clock
IO.print("stretch tree of depth ", stretchDepth, " check: ",
new Tree(0, stretchDepth).check)
Tree.new(0, stretchDepth).check)
var longLivedTree = new Tree(0, maxDepth)
var longLivedTree = Tree.new(0, maxDepth)
// iterations = 2 ** maxDepth
var iterations = 1
@@ -41,7 +41,7 @@ var depth = minDepth
while (depth < stretchDepth) {
var check = 0
for (i in 1..iterations) {
check = check + new Tree(i, depth).check + new Tree(-i, depth).check
check = check + Tree.new(i, depth).check + Tree.new(-i, depth).check
}
IO.print((iterations * 2), " trees of depth ", depth, " check: ", check)