clojure - How does this destructuring let form work? -
consider form:
(def v [42 "foo" 99.2 [5 12]]) i've read if have declare unused variables in let form should denote them _ in destructuring form:
(let [[x _ _ [y z]] v] (+ x y z)) my question how assignment _ happens? since not throw exception assume 2nd _ overrides first 1 i'm not sure. how work?
this use of _ purely conventional: clojure's point of view, it's regular symbol can used name local. so, can check value of _ directly confirm understanding:
(let [[x _ _ [y z]] v] _) ;= 99.2 as goes on under hood, simplest way check macroexpand let form:
(macroexpand-1 '(let [[x _ _ [y z]] v] _)) the result of above, reformatted clarity, looks this:
(let* [vec__7 v x (clojure.core/nth vec__7 0 nil) _ (clojure.core/nth vec__7 1 nil) _ (clojure.core/nth vec__7 2 nil) vec__8 (clojure.core/nth vec__7 3 nil) y (clojure.core/nth vec__8 0 nil) z (clojure.core/nth vec__8 1 nil)] _) so second _ shadows first one.
let* implementation detail behind let; special form directly understood compiler, let macro adds destructuring support.
Comments
Post a Comment