-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug: -if-let
mismatch between documentation and behavior
#392
Comments
The (-let (((a b c d) nil)) ;; only this one would fail -if-let
(message "%s" (list a b c d)))
(-let (((a b c d) (list 1)))
(message "%s" (list a b c d)))
(-let (((a b c d) (list 1 2 3 4 5)))
(message "%s" (list a b c d))) |
By passes do you mean
This fails (-if-let ((a b c d) (list 1))
(message "%s" (list a b c d))) So |
Oh yea, so I got myself confused. What you say is correct. The We should fix the documentation. There are cases where for example |
Gotcha, so that's the expected behavior then. Maybe something like: |
This was actually confusing coming from Clojure, where Here's the equivalent example (note: gensyms cleaned up for readability) (if-let [[foo bar] '(3)] :true :false) ;; => :true
;; macroexpands to:
(let [temp# '(3)]
(if temp#
(let* [vec# temp#
foo (nth vec# 0 nil)
bar (nth vec# 1 nil)]
:true)
:false)) Clojure also has a related set of macros (defmacro -if-some (var-val then &rest else)
"If VAL evaluates to non-nil, bind it to VAR and do THEN,
otherwise do ELSE.
Note: binding is done according to `-let'.
\(fn (VAR VAL) THEN &rest ELSE)"
(declare (debug ((sexp form) form body))
(indent 2))
(let* ((sym (make-symbol "input0"))
(bindings (dash--match (car var-val) sym)))
`(let ((,sym ,(cadr var-val)))
(if ,sym
(let* ,bindings
,then)
,@else)))) (-if-some ((a b) '(3))
(list a b)
'false)
;; => (3 nil)
;; =macroexpand=>
(let ((input0 '(3)))
(if input0
(let* ((--dash-source-97-- input0))
(a (pop --dash-source-97--))
(b (car --dash-source-97--)))
(list a b))
'false)) |
The documentation for
-if-let
reads as follows:If VAL evaluates to non-nil, bind it to VAR and do THEN, otherwise do ELSE.
However,
-if-let
's behavior does not match this, and instead appears to matchagainst (part of)
var
:Thanks for the great package!
The text was updated successfully, but these errors were encountered: