Returning a local variable currently copies, and avoiding the copy requires either an explicit return move(a); or use of named returns. The compiler could however transform returned local variables into safe named returns or moves on the user's behalf, like most C++ compilers do. For example,
func() {
foo();
var a = bar();
bas();
var b = zim();
zang();
return a, b, zung();
}
could be transformed into
func() --> a:Bar, b:Zim, c:Zung {
foo();
a <-- bar(); onerror destroy(a);
bas();
b <-- zim(); onerror destroy(b);
zang();
c <-- zung(); return;
}
In cases when a local variable cannot be safely emplaced in the return value, automatic move optimization can be used as a more general optimization.
Returning a local variable currently copies, and avoiding the copy requires either an explicit
return move(a);or use of named returns. The compiler could however transform returned local variables into safe named returns or moves on the user's behalf, like most C++ compilers do. For example,could be transformed into
In cases when a local variable cannot be safely emplaced in the return value, automatic
moveoptimization can be used as a more general optimization.