beginners

Karl Xaver 2025-04-28T12:08:48.955359Z

Im a bit confused about case. Why does this work:

(defn reader=>channel [^Reader rdr ch]
  (loop []
    (let [c (.read rdr)]
      (if (= c -1)
        (t/log! "reader-loop shutdown: received -1")
        (do (a/>!! ch c)
            (recur))))))
But this fails (-1 branch never gets hit and the -1s are spammed onto the channel)?
(defn reader=>channel [^Reader rdr ch]
  (loop []
    (let [c (.read rdr)]
      (case c
        -1 (t/log! "reader-loop shutdown: received -1")
        (do (a/>!! ch c)
            (recur))))))

rolt 2025-04-28T12:25:30.839129Z

both versions work in my repl, what's your environment ?

rolt 2025-04-28T12:28:25.976469Z

(defn reader=>out [^java.io.Reader rdr]
    (loop []
      (let [c (.read rdr)]
        (case c
          -1 (println "reader-loop shutdown: received -1")
          (do (prn c)
              (recur))))))
(reader=>out (java.io.StringReader. "abc"))

97
98
99
reader-loop shutdown: received -1

Karl Xaver 2025-04-28T12:46:53.404879Z

Thank you for the sanity check! I should have tried it "atomically" like that myself. Can't reproduce the original error anymore, even though it persisted accross repl restarts. There must've been some stray form that got evaluated when loading the file. Time to develop a tidier workflow :o

rolt 2025-04-28T14:30:06.808929Z

> it persisted across repl restarts scary. I've had some stale compiled files trip me up in the past but it's been a while since I've encountered this kind of issue. A lot of those went away by adding "%s" to the target path in leiningen if that's what you're using. The good thing is that it's usually a bit easier in clojure to try thing "atomically" when weird things happen.

Karl Xaver 2025-04-28T14:40:48.053309Z

Oh, good to know that can happen. The thing I'm playing with blows up a lot - maybe because of JNI stuff going on under the hood - and I have to force kill the repl. Though I suspect human error in this particular case.