Skip to content

Commit

Permalink
Adding test of return bubbling up correctly inside for loops and nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly committed Sep 3, 2024
1 parent 0136f7f commit 193e3e0
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions examples/for.gr
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,25 @@ for v = [2,-5,7] {println("v",v)}

// For strings iterations is on runes - demonstrate continue (skip the "b")
for c = "ab😀" {if c=="b" {continue} println("rune",c)}

// -- test of return's scope --

innerRetVal = "abc"
outerRetVal = "xyz"
res = () => {
inner = () => {
// for loop with a stop condition
for true {println("(not) infinite loop"); return innerRetVal}
// unreachable code, return inside the for should bubble up to the lambda level
println("this will not be printed")
return "def"
}()
if inner != "abc" {
error("unexpected inner return value", inner)
}
return outerRetVal
}()

if res != "xyz" {
error("unexpected outer return value", res)
}

0 comments on commit 193e3e0

Please sign in to comment.