haxe icon indicating copy to clipboard operation
haxe copied to clipboard

Closures are not inlined when used through another inline closure

Open Speedphoenix opened this issue 1 year ago • 0 comments

In this situation

class Extensions {
	inline static public function findMin<T>(a:Array<T>, f:T->Float) {
		var minVal = Math.POSITIVE_INFINITY;
		var minItem = null;
		for (item in a) {
			var v = f(item);
			if (v < minVal) {
				minVal = v;
				minItem = item;
			}
		}
		return {item: minItem, val: minVal};
	}

	inline static public function findMaxValue<T>(a:Array<T>, f:T->Float):Float {
		return -findMin(a, i -> -f(i)).val;
	}
}

class Test {
	static function main() {
		var arr = [5, 4, 65, 8];
		trace(Extensions.findMaxValue(arr, e -> e * 2));
	}
}

The e -> e * 2 closure is not inlined, even though it's only used in inline functions

On Try Haxe, the JS Source still shows

let arr = [5,4,65,8];
let f = function(e) {
	return e * 2;
};
let minVal = Infinity;
let _g = 0;
while(_g < arr.length) {
	let v = -f(arr[_g++]);
	if(v < minVal) {
		minVal = v;
	}
}
console.log("Test.hx:23:",-minVal);

Speedphoenix avatar Apr 04 '24 09:04 Speedphoenix