java

2024-08-05T20:57:09.337509Z

My understanding of https://clojure.atlassian.net/browse/CLJ-2799 is that in clojure 1.12 we'll be able to pass functions in any place where an interface annotated with @FunctionalInterface is expected. But that annotation must be present in the java interface code for it to work. Is there any particular reason in Java NOT to use that annotation in situations where the intention is to use the things as functions? I'm trying out https://github.com/openjdk/jextract and it generates code that looks like:

public class wl_notify_func_t {

    ...

    /**
     * The function pointer signature, expressed as a functional interface
     */
    public interface Function {
        void apply(MemorySegment listener, MemorySegment data);
    }
It seems intended to be used as a "functional interface" since those words are in the comment, but it lacks the annotation. What reasons are there in java to add or not add that annotation in Java?

Alex Miller (Clojure team) 2024-08-05T21:06:57.175069Z

FunctionalInterfaces can only have one method, so marking it this way means not changing your mind and adding another method later

Alex Miller (Clojure team) 2024-08-05T21:07:17.106589Z

but if that is your intent, then no reason not to

2024-08-05T21:13:21.454119Z

Ah, makes sense. I think in java you can use lambdas any time there's a "Single Abstract Method" interface. Do you know offhand if those lambda usages will also break if a method is later added to the interface? I'm thinking they would, and thus if lambda functions are used in docs and such, it strengthens the case for just making the thing a FunctionalInterface?

Alex Miller (Clojure team) 2024-08-05T21:20:48.642739Z

it depends :)

Alex Miller (Clojure team) 2024-08-05T21:21:30.555969Z

if they are still distinct by arity then this is not a breaking change

Alex Miller (Clojure team) 2024-08-05T21:22:15.008889Z

I think! we went through some examples like this but I may be misremembering

2024-08-05T21:28:32.739459Z

Thanks, good to know

Alex Miller (Clojure team) 2024-08-05T21:29:49.971059Z

actually that may be wrong - I think it would break use of a lambda in that case

👀 1
littleli 2024-08-06T09:39:09.777809Z

...and then default interface methods enter the bar 🙂