core-async

Ben Sless 2020-05-17T17:22:57.244700Z

Hi all, is there a reason this snippet throws Could not resolve var: -has?

(macroexpand-1
 '(go
    (let [[v ch :as ret] (a/alts! [-has -insert])]
      ret)))

alexmiller 2020-05-17T17:35:53.245400Z

What are -has and -insert here? They should be channels

Ben Sless 2020-05-17T17:39:24.247600Z

They are. Taken from vectors of channels, like (has i) with has being (vec (repeatedly (+ 1 limit) chan)), and -insert is similarly defined. I drilled down into this trying to debug why I was takeing from nil in a much larger piece of code:

(defn rec-set
  [limit]
  (let [has (vec (repeatedly (+ 1 limit) chan))
        insert (vec (repeatedly (+ 1 limit) chan))
        s0 (chan)]
    (loop [i 0]
      (when (< i limit)
        (let [i+1 (inc i)
              -has (has i)
              +has (has i+1)
              -insert (insert i)
              +insert (insert i+1)]
          (go
            (let [n
                  (loop []
                    (a/alt!
                      -has ([_] (>! s0 false) (recur))
                      -insert ([v] v)))]
              (loop [n n]
                (a/alt!
                  -has
                  ([m]
                   (cond
                     (<= m n) (>! s0 (= m n))
                     (> m n)  (>! +has m))
                   (recur n))
                  -insert
                  ([m]
                   (cond
                     (< m n) (do (>! +insert n) (recur m))
                     (> m n) (do (>! +insert m) (recur n))
                     (= m n) (recur n)))))))
          (recur (inc i)))))
    {:has (has 1) :insert (insert 1) :s0 s0})) 
Still trying to find the error here

2020-05-17T17:47:57.249800Z

macroexpand-1 may just not not work on go macro calls with free variables (because the go macro is very complicated internally, even doing its own macro expansion)

2020-05-17T17:55:26.254800Z

Which is to say, symbol resolution errors typically occur later in the compilation process than macro expansion, so you can usually expand macros with free symbols just fine, but the go macro is a compiler in a macro, which means when macro expanding it you can get errors from later stages in the compilation process. Just a guess, I am not at a repl to test it out

Ben Sless 2020-05-17T18:06:57.255100Z

damn, makes debugging the big form harder

2020-05-17T18:08:19.256500Z

If that is the case you just need to make the symbols not free

2020-05-17T18:09:09.258500Z

But I doubt macroexpand core async forms is ever going to make it easier to understand

Ben Sless 2020-05-17T18:09:15.258700Z

That would be tricky because the error I'm getting is that I'm trying to take! from nil, meaning one of the alt s is borked

Ben Sless 2020-05-17T18:10:00.259500Z

But if I just print all the -+has and -+insert chans non of them are nil

2020-05-17T18:11:24.260300Z

I would start by sticking asserts before each alt

Ben Sless 2020-05-17T18:11:47.260500Z

That's an excellent idea

Ben Sless 2020-05-17T18:12:21.261100Z

won't it throw in the executor pool, though?

2020-05-17T18:12:31.261700Z

That is fine

2020-05-17T18:12:50.262400Z

And I would double check that your error is actually coming from that code

2020-05-17T18:14:08.263100Z

I forget if vectors as functions behave like get or nth

2020-05-17T18:14:40.263800Z

Get returns nil for unkown indices, nth throws

2020-05-17T18:16:30.265600Z

So if vectors as functions act like get, you can replace with calling nth to get earlier errors to track things back closer to the source

Ben Sless 2020-05-17T18:17:55.265800Z

omg

Ben Sless 2020-05-17T18:17:57.266100Z

I found the error

Ben Sless 2020-05-17T18:17:59.266400Z

it's so silly

Ben Sless 2020-05-17T18:18:21.266800Z

My error was in the api. The code is good

Ben Sless 2020-05-17T18:19:16.267200Z

Thanks for giving it a look over 🙃

2020-05-17T18:22:43.269100Z

Looking over macro expanded go blocks is extremely tedious, and ideally only something to do if you are sure there is a bug in the go macro

Ben Sless 2020-05-17T18:27:13.269700Z

Well, the bug was as it most often is, between the chair and the keyboard