Fork me on GitHub
#java
<
2023-06-30
>
Ben Sless09:06:40

How can I pass a static method as an argument?

jkxyz10:06:05

If you mean as an argument to a Clojure function, wrap it in a function like #(UUID/randomUUID)

Ben Sless10:06:46

As an argument to a java static method 🙂

jkxyz10:06:05

I would hesitantly suggest using a lambda expression, but I've not used them in Java 😄

Ben Sless10:06:13

That seems right

Alex Miller (Clojure team)11:06:30

In Java, as a Class::method method reference

Alex Miller (Clojure team)11:06:33

From Clojure right now, you’ll need to reify a Java functional interface (but this is an area of active work)

Ben Sless12:06:31

This is a pure java question What's the type of that argument? How do I invoke the method I passed as an argument?

Alex Miller (Clojure team)13:06:01

it depends on the method

Alex Miller (Clojure team)13:06:21

it's being treated as some kind of functional interface, so a method that is Object f(Object) could be typed as a java.util.function.Function for example

Ben Sless13:06:40

clojure.lang.Util/equiv and clojure.lang.Util/equals 🙂

Ben Sless13:06:09

BiPredicate?

Alex Miller (Clojure team)13:06:43

what are you passing it to?

Alex Miller (Clojure team)13:06:44

BiPredicate is probably the best match there

Ben Sless14:06:56

I have a couple of static methods whose only difference is which static method they're calling, equiv/equals

emccue17:06:01

You can make your own interface

emccue17:06:01

interface EqualityMethod {
    static EqualityMethod EQUIV = clojure.lang.Util::equiv;
    static EqualityMethod EQUALS = clojure.lang.Util::equals;
    boolean areEquivalent(Object a, Object b);
}

Ben Sless10:06:46

As an argument to a java static method 🙂