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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user