You must use <! and >! from within go directly - you cannot use them inside a function or macro.
This is because the go macro walks the abstract syntax tree to turn it into a state machine and as it does this, it turns the calls to <! and >! into something else (in fact, if you look at the source, you will see that <! and >! themselves actually don't do anything! [1], because the go macro replaces it with the real code). The macro doesn't know how to walk through function or macro calls, so cannot find <! and >! contained within and therefore cannot turn them into the proper code. The only way you could do this is do something similar to the go-loop macro: have your macro return a go macro AFTER walking the code passed in replacing your special dispatch code with normal calls to <!. Or, what I do: encapsulate my go block in a function which takes as arguments functions to dispatch to (but the function itself handles the go block and dispatch, so I don't need to write this each time). [1] The ClojureScript source for <! is: (defn <! "takes a val from port. Must be called inside a (go ...) block. Will return nil if closed. Will park if nothing is available. Returns true unless port is already closed" [port] (assert nil "<! used not in (go ...) block")) On 21 July 2014 18:16, Tom Locke <[email protected]> wrote: > Hi All, > > I had a terrible time trying to write a convenience macro for reading from > a core.async channel. > > The idea was to make it easier to do the common "dispatch on first part of > a message" pattern, as in: > > (let [[msg & args] (<! my-channel)] > (case msg > :msg1 (let [[a b] args] ...) > :msg2 (let [[x] args] ...))) > > After a long struggle I came to the conclusion that there is some problem > with using macros inside a go block, or at least using <! inside a macro. > Is that right? Is this documented anywhere? > > Thanks > > Tom > > -- > Note that posts from new members are moderated - please be patient with > your first post. > --- > You received this message because you are subscribed to the Google Groups > "ClojureScript" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/clojurescript. > -- Note that posts from new members are moderated - please be patient with your first post. --- You received this message because you are subscribed to the Google Groups "ClojureScript" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/clojurescript.
