clr

Gishu Pillai 2025-05-27T15:40:09.799799Z

First post - I have been tinkering with Clojure for a while somehow missed ClojureCLR. => (defn worker [state] (println (.ManagedThreadId Thread/CurrentThread)) (Thread/Sleep 2000) (println "Worker done processing =>" state)) => (ThreadPool/QueueUserWorkItem worker) Execution error (InvalidCastException) at System.Runtime.CompilerServices.CastHelpers/ChkCast_Helper (NO_FILE:0). Unable to cast object of type 'Call_dot_net$worker__24966' to type 'System.Threading.WaitCallback' On JVM, any clojure function can be passed to a ThreadPool object AFAIK. In .Net, I am getting this error because the method expects a Delegate. Is there any documentation on interfaces supported by all functions? How do I get this code to work? (Reading Joy of Clojure's chapter on concurrency)

Gishu Pillai 2025-05-28T10:31:54.323879Z

Thanks! Is there a page that lists the Java vs .Net differences w.r.t. Interop? https://github.com/clojure/clojure-clr/blob/master/Clojure/Clojure/Lib/IFn.cs public interface IFn // Callable, Runnable -- no equivalents

Gishu Pillai 2025-05-28T11:32:11.145879Z

e.g. How would one go about calling an API which has out parameters ? public static void GetMaxThreads(out int workerThreads, out int completionPortThreads);

jamesd3142 2025-05-30T00:17:53.796699Z

The wiki page has some good info about this https://github.com/clojure/clojure-clr/wiki/ByRef-and-params#byref-parameters

jamesd3142 2025-05-30T00:29:50.267179Z

I think you'd want:

(let [workerThreads 0 completionPortThreads 0]
  (System.Threading.ThreadPool/GetMaxThreads (by-ref workerThreads) (by-ref completionPortThreads))
  [workerThreads completionPortThreads])

jamesd3142 2025-05-28T00:30:26.024799Z

You can use gen-delegate like this:

=> (import '[System.Threading Thread ThreadPool WaitCallback])
=> (def worker
  (gen-delegate WaitCallback [state]
     (println (.ManagedThreadId Thread/CurrentThread))
     (Thread/Sleep 2000)
     (println "Worker done processing =>" state)))
=> (ThreadPool/QueueUserWorkItem worker)

✅ 1