revise defer to use heap allocations when running a variable number of times

This commit is contained in:
Jaden Weiss
2019-11-22 13:17:25 -05:00
committed by Ayke
parent a4fa41b49d
commit eee1b995f6
3 changed files with 61 additions and 3 deletions
+9
View File
@@ -41,6 +41,9 @@ func main() {
// deferred functions
testDefer()
// defers in loop
testDeferLoop()
// Take a bound method and use it as a function pointer.
// This function pointer needs a context pointer.
testBound(thing.String)
@@ -85,6 +88,12 @@ func testDefer() {
println("deferring...")
}
func testDeferLoop() {
for j := 0; j < 4; j++ {
defer deferred("loop", j)
}
}
func deferred(msg string, i int) {
println(msg, i)
}
+4
View File
@@ -4,6 +4,10 @@ Thing.Print: foo arg: bar
...run as defer 3
...run closure deferred: 4
...run as defer 1
loop 3
loop 2
loop 1
loop 0
bound method: foo
thing inside closure: foo
inside fp closure: foo 3