Fork me on GitHub
#clojure-uk
<
2024-05-24
>
maleghast08:05:24

madainn mhath :flag-scotland:

seancorfield15:05:55

We have Clojure 1.12 Alpha 12 in QA at work. So far I've only found one SAM functional interface usage to replace.

seancorfield15:05:09

If you return a reify it's not really worth changing the code, since you have to use let to force the coercion...

dharrigan15:05:15

I do look forward to when the next version of Clojure comes out - not quite sure I would make much use of the SAM functionality - but we'll see!

seancorfield15:05:59

In a lot of places where you might reify a Java interface and provide a single method? 🙂

seancorfield15:05:24

Currently, it has to be annotated with @FunctionalInterface for Clojure, but Java doesn't require that for lambdas so there's been some discussion of expanding Clojure's rule to match Java which would open up a lot more possibilities -- including stuff with clojure.lang.* which would be helpful for us.

dharrigan15:05:01

A quick rg of our source just reveals a few places of reify

seancorfield15:05:12

We went from:

(defn- wildcard-filter
  "Given a regex, return a FilenameFilter that matches."
  [re]
  (reify java.io.FilenameFilter
    (accept [_ dir name] (not (nil? (re-find re name))))))

(defn directory-list
  "Given a directory and a regex, return a sorted seq of matching filenames."
  [dir re]
  (sort (.list (io/file dir) (wildcard-filter re))))
to
(defn directory-list
  "Given a directory and a regex, return a sorted seq of matching filenames."
  [dir re]
  (sort (.list (io/file dir) #(not (nil? (re-find re %2))))))
in one place.

dharrigan15:05:12

all of (reify Thread$UncaughtExceptionHandler...)

dharrigan15:05:36

that's a great code saving 🙂

seancorfield15:05:24

All those reify Thread$UncaughtExceptionHandler could be switched to lambdas -- it's a functional interface.

dharrigan15:05:09

so, that'l be good when 1.12 comes in 🙂 However, we only have 1 instance of that in our project 🙂

seancorfield15:05:39

In another place we went from:

client (TelemetryClient/create
                ^Supplier
                (reify Supplier
                  (get [_] (OkHttpPoster. (Duration/of 10 ChronoUnit/SECONDS))))
                ^String
                nr-insert-key)
to
client (TelemetryClient/create
                (delay (OkHttpPoster. (Duration/of 10 ChronoUnit/SECONDS)))
                ^String nr-insert-key)
(but that's in an early 1.12 alpha).

seancorfield15:05:38

And we're probably going to replace a couple of these as well:

(case (:ws-type msg)
                    :text (.handleAsync
                           (.sendText a (:data msg) true)
                           (reify
                             BiFunction
                             (apply [_ _ b]
                               (when b
                                 (logger/error b))
                               (ws-send-loop a)))))

seancorfield16:05:43

Combined with the method values stuff, it's all nice little savings...