haxe icon indicating copy to clipboard operation
haxe copied to clipboard

Non capturing local functions

Open Aidan63 opened this issue 5 months ago • 2 comments

This idea has been floated a few times in the coroutine stuff, basically some way to mark a local function as not being allowed to capture variables to prevent GC retention issues and potentially allow some targets to perform some optimisations.

I quite like the C# syntax for this which is to use the static keyword. i.e. a haxe version would be

function main() {
	var i = 0;

	static function foo() {
		// compile error
		i = 10;
	}

	foo();
}

I think this make some sense since we have static local variables, not sure about the anonymous function syntax though...

function main() {
	var i = 0;

	final foo = static () -> {
		// compile error
		i = 10;
	}

	foo();
}

Could also probably be done with some metadata, not sure whats easier / best. Wasn't sure if this should be a formal haxe evolution, but since I hasn't really been given any thought outside of "that seems nice" I thought I'd open an issue here first.

Aidan63 avatar Jul 01 '25 21:07 Aidan63

I like this idea a lot, will have to think it through though.

Simn avatar Jul 02 '25 04:07 Simn

I like it too, but I'm really not sure about the use of static for this

kLabz avatar Jul 02 '25 05:07 kLabz