aboutsummaryrefslogtreecommitdiff
path: root/test/defer.tm
blob: 121878b1370e2d06e5a7025aadb752ecc20b3f35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
func main():
    x := 123
    nums := @[:Int]
    do:
        defer:
            nums:insert(x)
        x = 999

    >> nums
    = @[123]
    >> x
    = 999

    defer:
        say("All done!")

    for word in ["first", "second", "third"]:
        defer:
            say("Got {word} deferred")

        if word == "second":
            say("<skipped>")
            skip
        else if word == "third":
            say("<stopped>")
            stop

        for i in 3:
            defer:
                say("Inner loop deferred {i}")

            if i == 2:
                say("<skipped inner>")
                skip
            else if i == 3:
                say("<stopped inner>")
                stop

            say("Made it through inner loop")

        say("Made it through the loop")
    
    >> thunk := func(return_early=no):
        say("Entering thunk")
        defer:
            say("Deferred thunk cleanup")

        if return_early:
            say("Returning early...")
            return

        say("Finished thunk")

    >> thunk(no)
    >> thunk(yes)

    >> defer_func(yes)
    >> defer_func(no)

func defer_func(return_early=no):
    say("Entering defer_func")
    defer:
        say("Deferred defer_func cleanup")

    if return_early:
        say("Returning early...")
        return

    say("Finished defer_func")