I’ll note that I did, at one point, contemplate having something like
fn foo<T, 'a: break(T)>(val: T) -> ! {
break 'a val;
}
fn bar() {
let x = 'a: {
foo<'a>(5)
};
println!("{}", x); // 5
}
Which is why I chose fsm!
for my version of it, as after all one of things it stands for is ‘finite-state machine’. Here’s your example translated:
let result = fsm! s1(42) {
s1(n: i32) => {
if n % 2 == 0 {
continue s3(n / 2, n + 1)
}
s2
},
s2 => s1(84),
s3(x: i32, y: i32) => match foo {
None => break x + y,
Some(n) => continue s1(n)
},
};
No new keywords necessary, and it can be easily read from the top, just like a match
expression. I think it could be implemented on top of loop
and match
as a procedural macro. A simplified version (without continue
and with slightly different initial state syntax) is easily implementable with a pattern-match macro.
(Aside: the funclets proposal for WebAssembly seems related, although it is a bit more general: by the look of it, it should be able to express things like binary tree traversal, unlike fsm!
.)