3 ms·
I figured out how return-as-a-value made sense only upon realizing that in the following code, fn funny(){ fn f(_x: ()){} f(return); }
by arjvik 1y ago
I figured out how return-as-a-value made sense only upon realizing that in the following code,
fn funny(){
fn f(_x: ()){}
f(return);
}
f() is never called because funny() returns before it gets called.
The reason you want return to be coercible to any type is so that you can write something like
let x: i32 = if y {
4
} else {
return; // type ! coerced into i32
}
And you pick the return value of ! because return never actually produces a value that is propagated on at runtime, it immediately exits the function.
(Note this all works even with returning a value)