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

FunctionalInterface annotation for JVM1.8+ on Kotlin functions #5396

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MukjepScarlet
Copy link

All Java built-in function types like BiFunction, IntProvider have this annotation. Also, this annotation is used by the GraalVM Polyglot library. It can only parse types with this annotation to a JavaScript function, that's why I want to add this.

All Java built-in function types like `BiFunction`, `IntProvider` have this annotation. Also, this annotation is used by the GraalVM Polyglot library. It can only parse types with this annotation to a JavaScript function, that's why I want to add this.
@MukjepScarlet
Copy link
Author

By the way, I think fun interface types can also have this in bytecode, but I'm not sure it's appropriate.

@udalov udalov self-assigned this Jan 27, 2025
@udalov
Copy link
Member

udalov commented Jan 27, 2025

Also, this annotation is used by the GraalVM Polyglot library. It can only parse types with this annotation to a JavaScript function

Can you please clarify where it is documented that this is the case? Or maybe there is a sample project where something breaks once you remove the @FunctionalInterface annotation?

@MukjepScarlet
Copy link
Author

CCBlueX/LiquidBounce#5454
Here is an example.

At first, I implement them as methods in object:
CCBlueX/LiquidBounce@3cd4546

Then I found that the Polyglot throws an exception on script execution:
[2.js] test2::Test2::enable threw [PolyglotException]: TypeError: execute on net.ccbluex.liquidbounce.script.bindings.api.ScriptContextProvide r$setupContext$1$1 failed due to: Message not supported.

After that I tried another functions like a simple val test: (Int, Int) -> Int = { a, b -> a+b }, this is also not available in script, but the val test2: BiFunction<Int, Int, Int> = BiFunction { a, b -> a+b } works.
So I guess there's difference between the Kotlin generated FunctionN interfaces and Java functional interfaces. It's this annotation.

After replace with a custom interface with this annotation, it works.

@udalov udalov added the Backend label Feb 1, 2025
@05nelsonm
Copy link

I'm wondering how this would affect android API 23 and below as these APIs are not available. Have you tested it?

@MukjepScarlet
Copy link
Author

MukjepScarlet commented Feb 11, 2025

Well, I'm not a Android developer, this is only tested on desktop JDK 8, 17 and 21.

@MukjepScarlet
Copy link
Author

Android API 23 seems to not support this annotation (because this is included in stdlib from Java 8)

@MukjepScarlet
Copy link
Author

MukjepScarlet commented Feb 11, 2025

So we might only use this if the class is existing. But as far as I know, latest Kotlin 2.x only supports Java 1.8 and higher versions.

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

Successfully merging this pull request may close these issues.

3 participants