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
+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