cursive icon indicating copy to clipboard operation
cursive copied to clipboard

Destructuring with as-> gives unresolved symbol warnings

Open Sardtok opened this issue 2 years ago • 1 comments

as-> uses let to assign values to symbols.

(as-> something x
  (do-stuff x 1 2)
  (do-something-else 3 x))

translates to:

(let [x something
      x (do-stuff x 1 2)]
  (do-something-else 3 x))

That means you can also do destructuring assignment, as long as all the expressions return a similar structure:

(as-> something {:keys [a b c] :as x}
  (assoc x :a (* a b))
  (do-something-fantastic b c x))

translating to:

(let [{:keys [a b c] :as x} something
      {:keys [a b c] :as x} (assoc x :a (*a b))]
  (do-something-fantastic b c x))

This isn't necessarily code I like much, but I've found this littered around our codebase, and I get unresolved warnings where they are used.

Sardtok avatar Jun 15 '23 11:06 Sardtok

Would love to see this added. It can be useful for grabbing some values from the threaded object:

(-> {:a 1}
    (assoc :b 2)
    (update :a inc)
    (as-> {:keys [a b] :as m} (assoc m :c (+ a b)))
    ...
    )

pmoriarty avatar Mar 03 '24 00:03 pmoriarty