clojure.lang.Atom cannot be cast to a java.lang.Num

580 views
Skip to first unread message

Dylan Gleason

unread,
May 27, 2014, 3:43:58 AM5/27/14
to clo...@googlegroups.com
I am trying to read a TCP request via an instance of DataInputStream and am running into this error with the following code:

(defn receive
  [socket]
  (with-open [reader (DataInputStream. (.getInputStream socket))]
    (let [length   (read-length reader)
          bytes-in (byte-array length)
          offset   (atom 0)
          index    (atom 0)]
      (while (not= -1 @offset)
        (reset! offset (.read reader bytes-in offset (- length index)))
        (reset! index (+ index offset))))))

For some reason, trying to update the value in offset appears to result in this casting error. I tried testing a similar expression at the REPL, and it works, e.g.

> (def blah (atom 0))
> (reset! blah 93)    ; no error

It seems odd that Clojure would complain about this, since "93" is an instance of java.lang.Long -- why would Clojure be so annoyingly anal-retentive when setting the atom to a java.lang.Num? Also, if anyone has any ideas on how to make achieve this without using atoms, and what the best approach would be (a recursive solution using loop-recur, perhaps?)

Ambrose Bonnaire-Sergeant

unread,
May 27, 2014, 3:51:00 AM5/27/14
to clojure
You're adding two atoms together here:  (reset! index (+ index offset))

Thanks,
Ambrose


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Philippe Guillebert

unread,
May 27, 2014, 5:15:30 AM5/27/14
to clo...@googlegroups.com

Also, you should be using loop/recur instead of mutating atoms here, this is the functional way.

You already laid out your 2 loop bindings, and a stop condition. This should be straightforward.

Philippe.

--

Dylan Gleason

unread,
May 28, 2014, 1:59:39 AM5/28/14
to clo...@googlegroups.com
Thank you, Ambrose. It appears I forgot to dereference both index and offset when trying to read the values associated with the atoms. Thanks Phillippe -- I went with your recursive solution and bypassed mutation via atoms altogether -- a much cleaner solution.


You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/DRxhey7Mx8Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages