Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using anonymous functions in operator expressions #12015

Open
wants to merge 5 commits into
base: development
Choose a base branch
from

Conversation

tobil4sk
Copy link
Member

@tobil4sk tobil4sk commented Feb 16, 2025

This was disabled to deal with #5854, however, with this patch it is possible to avoid that issue without disallowing other types of expressions. For good measure, I have added a few more tests for #5854 to make sure this doesn't break that.

This makes anonymous functions nicer to use with operator overloading which is useful for functional style programming. It also fixes their usage with >>, which tink_core overloads for chaining futures.

See #10782

Adds more cases that shouldn't be parsed as immediately invoked
functions.
Inline block functions are already handled here, so it is more
consistent to handle all block leve functions at this point.
@Simn
Copy link
Member

Simn commented Feb 17, 2025

I love this issue because I can come up with contrived examples where the grammar breaks.

Involuntary array access

class C {
	public function new() {}

	public function call() {
		trace("called!");
	}
}

function main() {
	var f = function() {
		trace("f!");
	}
	[new C()][0].call(); // Array access is not allowed on () -> Void
}

Prefix turned postfix

function main() {
	var i = 0;
	var f = function() {
		trace("f!");
	}
	--i; // Missing ;
}

Abstract - overload

abstract NegativeCounter(Int) {
	public function new() {
		this = 0;
	}

	@:op(-A) inline function decrease() {
		--this;
	}
}

function main() {
	var c = new NegativeCounter();
	var f = function() {
		trace("f!");
	} // () -> Void should be Int
	-c;
}

The first two can be fixed by handling them like the POpen case. The third one might be an acceptable loss.

@Simn
Copy link
Member

Simn commented Feb 19, 2025

Here's a weird one just so we can say we considered it:

function main() {
	var f = function() {
		trace("f!");
	}
	<xml></xml> // Unterminated markup literal
}

My opinion on this is if it doesn't break Shiro Games then it's not a problem, so we should check with them before merging.

On a side note, the error seems odd too, I don't know if a plain </xml> should complain about being unterminated...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants