Get static methods fully working.

They are compiled as local variables defined in an implicit
scope surrounding the class. This has the right semantics and,
better, means that there's no VM support needed for them.

They're purely syntax sugar.
This commit is contained in:
Bob Nystrom
2014-01-19 13:01:51 -08:00
parent 2a68c8ee1d
commit 83b5968340
15 changed files with 307 additions and 81 deletions
+3
View File
@@ -6,3 +6,6 @@ class Foo {
var foo = new Foo
IO.print(foo is Foo) // expect: true
IO.print(foo.toString) // expect: Foo
// TODO: Get rid of this. If you're defining a class, it's because you have
// some state to initialize. if you don't, it shouldn't be a class.
+5
View File
@@ -0,0 +1,5 @@
class Foo {
static bar {
fn _field = "wat" // expect error
}
}
+5
View File
@@ -0,0 +1,5 @@
class Foo {
static bar {
_field = "wat" // expect error
}
}
@@ -0,0 +1,14 @@
// Refering to an instance method in a nested static class should *not* walk
// out to find the nearest enclosing instance method. We could make that work,
// but it's confusing to users, and would require some tricky work to make sure
// the enclosing instance is closed over.
class Outer {
foo {
class Inner {
static bar {
_field = "nope" // expect error
}
}
}
}
+16
View File
@@ -0,0 +1,16 @@
class Foo {
static initialize { __field = "Foo field" }
static closeOverGet {
return fn { return __field }
}
static closeOverSet {
return fn { __field = "new value" }
}
}
Foo.initialize
IO.print(Foo.closeOverGet.call) // expect: Foo field
Foo.closeOverSet.call
IO.print(Foo.closeOverGet.call) // expect: new value
+5
View File
@@ -0,0 +1,5 @@
class Foo {
static write { IO.print(__field) }
}
Foo.write // expect: null
+25
View File
@@ -0,0 +1,25 @@
class Foo {
set(a, b, c, d, e) {
__a = a
__b = b
__c = c
__d = d
__e = e
}
write {
IO.print(__a)
IO.print(__b)
IO.print(__c)
IO.print(__d)
IO.print(__e)
}
}
(new Foo).set(1, 2, 3, 4, 5)
(new Foo).write
// expect: 1
// expect: 2
// expect: 3
// expect: 4
// expect: 5
+25
View File
@@ -0,0 +1,25 @@
class Foo {
static set(a, b, c, d, e) {
__a = a
__b = b
__c = c
__d = d
__e = e
}
static write {
IO.print(__a)
IO.print(__b)
IO.print(__c)
IO.print(__d)
IO.print(__e)
}
}
Foo.set(1, 2, 3, 4, 5)
Foo.write
// expect: 1
// expect: 2
// expect: 3
// expect: 4
// expect: 5
+34
View File
@@ -0,0 +1,34 @@
class Outer {
static staticMethod {
__field = "outer"
IO.print(__field) // expect: outer
class Inner {
static staticMethod {
__field = "inner"
IO.print(__field) // expect: inner
}
}
Inner.staticMethod
IO.print(__field) // expect: outer
}
instanceMethod {
__field = "outer"
IO.print(__field) // expect: outer
class Inner {
instanceMethod {
__field = "inner"
IO.print(__field) // expect: inner
}
}
(new Inner).instanceMethod
IO.print(__field) // expect: outer
}
}
Outer.staticMethod
(new Outer).instanceMethod
+1
View File
@@ -0,0 +1 @@
__field = "wat" // expect error
+8
View File
@@ -0,0 +1,8 @@
class Foo {
static write { IO.print(__field) } // Compile a use of the field...
static init { __field = "value" } // ...before an assignment to it.
}
// But invoke them in the right order.
Foo.init
Foo.write // expect: value
+4
View File
@@ -4,3 +4,7 @@ class Foo {
}
IO.print((new Foo).bar.baz) // expect: baz
// TODO: Test that "this" is the class when in a static method.
// (Or disallow "this" in statics? It's useful since statics are
// inherited.)