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

[jvm] automatic conversion of Haxe functions to Java functional interfaces #9576

Closed
Gama11 opened this issue Jun 13, 2020 · 6 comments
Closed
Assignees
Labels
enhancement platform-jvm Everything related to JVM

Comments

@Gama11
Copy link
Member

Gama11 commented Jun 13, 2020

In Java, any interface with a single function is considered a "functional interface" that can be "instantiated" with lambda syntax. So for interop purposes, it would be quite nice if the following magically worked:

import java.util.function.Function;

function foo(f:Function<Int, Int>) {
	trace(f.apply(3));
}

function main() {
	foo(i -> i * 2);
}

(with foo() usually being loaded from a .jar)

@Gama11 Gama11 added enhancement platform-java Everything related to Java platform-jvm Everything related to JVM and removed platform-java Everything related to Java labels Jun 13, 2020
@Gama11 Gama11 changed the title [java] automatic conversion of Haxe functions to Java functional interfaces [jvm] automatic conversion of Haxe functions to Java functional interfaces Jun 13, 2020
@Simn Simn self-assigned this Jun 15, 2020
@Simn
Copy link
Member

Simn commented Jun 15, 2020

I have added Function as one of the hardcoded implicit interfaces. I was wondering if we should solve this in a broader manner, but I'm not sure how exactly that would work. Maybe it's more pragmatic to just go through the list of java.util.function interfaces and add them all like I did in this commit.

There's also a test case. The example for now requires a cast because I have no idea how to satisfy unification here.

@Gama11
Copy link
Member Author

Gama11 commented Jun 15, 2020

Hm, too bad you need a cast there.

but I'm not sure how exactly that would work

It'd have to be done dynamically for, any single-method-interface I guess?

@Simn
Copy link
Member

Simn commented Jun 15, 2020

@Gama11
Copy link
Member Author

Gama11 commented Jun 15, 2020

The default methods probably don't count.

@Simn
Copy link
Member

Simn commented Dec 1, 2021

Now that we actually track these default methods there's a chance to fix this. On the generator side, we will have to identify all single-method interfaces and then check each of these against each function class. That shouldn't be very expensive because it is going to fail very early very often.

The other problem is the unification part...

Simn added a commit that referenced this issue Dec 1, 2021
@Simn Simn removed this from the Backlog milestone Mar 24, 2023
Simn added a commit that referenced this issue Mar 24, 2023
* [jvm] deal with functional interfaces

see #9576

* [typer] handle functional interface assignments

* Added tests for Java functional interfaces.

* fix test

---------

Co-authored-by: EliteMasterEric <ericmyllyoja@gmail.com>
@Simn
Copy link
Member

Simn commented Mar 24, 2023

Implemented in #11019.

@Simn Simn closed this as completed Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement platform-jvm Everything related to JVM
Projects
None yet
Development

No branches or pull requests

3 participants