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

Request: for Kotlin, offer to replace "interface" with "fun interface" when possible #11

Open
AndroidDeveloperLB opened this issue Dec 28, 2021 · 10 comments
Labels
enhancement New feature or request wontfix This will not be worked on

Comments

@AndroidDeveloperLB
Copy link

I think it's enough to just check if the number of functions in the interface is just one, not sure.

@Miha-x64
Copy link
Owner

Yes, but how this is useful?
AFAIK, the only reason to use fun interface is to enable SAM while avoiding boxing of primitives and inline classes. Is this a common scenario? Or what am I missing?

@AndroidDeveloperLB
Copy link
Author

Aren't those advantages enough?
Also, it makes code shorter :)
I think most people don't even know about this. Having an inspection could encourage knowing about it.

@AndroidDeveloperLB
Copy link
Author

Here, a talk about it:
https://stackoverflow.com/a/59446061/878126

@Miha-x64
Copy link
Owner

Still, why prefer fun interface over functional type? For example, the former cannot be inlined.
This gives me idea of another inspection: if a short non-recursive non-override function never touches anything private and accepts an instance of fun interface which is called in-place, the function should be marked as inline, and fun interface should be replaced with a functional type.

@AndroidDeveloperLB
Copy link
Author

Can you please provide an example (code snippet) of when it isn't a nice thing to have?

@AndroidDeveloperLB
Copy link
Author

AndroidDeveloperLB commented Dec 29, 2021

I personally prefer it this way. I think on Java it's automatic, no?
Can you please at least make it optional?

@Miha-x64
Copy link
Owner

Miha-x64 commented Jan 7, 2022

when it isn't a nice thing to have?

Well, there's no technical difference between fun interfaces and grumpy ones. What I mean is

interface A { fun run() }
fun interface B { fun run() }
typealias C = () -> Unit

inline fun a(/*actually noinline*/ a: A)
inline fun a(/*actually noinline*/ b: B)
inline fun a(c: C) // truly inline

NB: this is just an observation from the IDE behavior. I haven't found any official information about this.

@AndroidDeveloperLB
Copy link
Author

By "noinline", you mean it's not inlined, right?

@Miha-x64
Copy link
Owner

Miha-x64 commented Jan 7, 2022

Right, and Kotlin has an according soft keyword: noinline.

@AndroidDeveloperLB
Copy link
Author

@Miha-x64 Oh I never used it. Sorry for that. Kotlin has some special keywords that I never used, and I don't think I even saw someone using...

@Miha-x64 Miha-x64 added wontfix This will not be worked on enhancement New feature or request labels Feb 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants