Search code examples
javaclojure

how to translate the double colon operator to clojure?


I discovered a new syntax for java 8 reading through the source for a framework I'm attempting to wrangle:

 Runtime.getRuntime().addShutdownHook(new Thread(Sirius::stop));

In clojure, I can translate it as:

(.addShutdownHook (Runtime/getRuntime) (Thread. ????))

But I'm not sure what to put for the ???



Solution

  • IFn extends Runnable, so you can just do

    #(Sirius/stop)
    

    It is worth noting that

    • You have to make the lambda. Clojure won't let you refer to it just as Sirius/stop
    • Java 8 functional interfaces under the hood work by making anonymous implementations of interfaces with only one method. So

      new Thread(Sirius::stop)

    is just syntactic sugar for

    new Thread(new Runnable {
        public void run() {
            Sirius.stop();
        }
    })
    

    If the interface in question isn't Runnable/Callable, you'll have to use the reify macro.