struct Range { a : int; b : int; } Range.metamethods.__for = function(iter,body) return quote var it = &iter while it.a < it.b do var i = it.a [body(i)] it.a = it.a + 1 end end end terra foo() var a = 0 -- This iterator is resumable, so you can break out of the loop and reenter. var iter = Range {0, 10} for i in iter do a = a + i break end for i in iter do a = a + i break end for i in iter do a = a + i end return a end assert(foo() == 10*9/2)