Investigating with hxScout, we found this interesting behavior of lambda functions in hxcpp -- that using a local variable inside a lambda function changes the variable to an Array under the hood:
That is, with this code:
static function local() {
var sum:Int = 0;
sum++;
trace(sum);
}
static function lambda() {
var sum:Int = 0;
function increment() { sum++; }
increment();
trace(sum);
}
The local() compiles to cpp:
int sum = (int)0;
(sum)++;
And lambda() compiles to cpp:
Array< int > sum = Array_obj< int >::__new().Add((int)0);
HX_BEGIN_LOCAL_FUNC_S1(hx::LocalFunc,_Function_1_1,Array< int >,sum)
int __ArgCount() const { return 0; }
Void run(){
{
(sum[(int)0])++;
}
return null();
}
HX_END_LOCAL_FUNC0((void))
There's surely a reason for this Array, perhaps to do with scope and C++ and whatnot, but since the lambda is intentionally modifying the outer variable sum, it seems like the Array shouldn't be necessary (in this case, anyway.)
This could imply that, in critical code sections, lambda usage is not ideal.
Hugh or anyone else know the reason?
Thanks!
-Jeff