Merge pull request #296 from cireu/fix-hash-opt-expander

Ensure `hash?` expander evaluates its arg only once.
master
Matus Goljer 7 years ago committed by GitHub
commit 15498602f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      dash.el
  2. 6
      dev/examples.el

@ -1831,7 +1831,9 @@ kv can be any key-value store, such as plist, alist or hash-table."
(defun dash-expand:&hash? (key source)
"Generate extracting KEY from SOURCE for &hash? destructuring.
Similar to &hash but check whether the map is not nil."
`(when ,source (gethash ,key ,source)))
(let ((src (make-symbol "src")))
`(let ((,src ,source))
(when ,src (gethash ,key ,src)))))
(defalias 'dash-expand:&keys 'dash-expand:&plist)

@ -1150,6 +1150,12 @@ new list."
(puthash :bar 2 hash)
(-let (((&hash :foo :bar) hash)) (list foo bar))) => '(1 2)
(-let (((&hash :foo (&hash? :bar)) (make-hash-table)))) => nil
;; Ensure `hash?' expander evaluates its arg only once
(let* ((ht (make-hash-table :test #'equal))
(fn (lambda (ht) (push 3 (gethash 'a ht)) ht)))
(puthash 'a nil ht)
(-let (((&hash? 'a) (funcall fn ht)))
a)) => '(3)
(-let (((_ &keys :foo :bar) (list 'ignored :foo 1 :bar 2))) (list foo bar)) => '(1 2)
;;; go over all the variations of match-form derivation
(-let (((&plist :foo foo :bar) (list :foo 1 :bar 2))) (list foo bar)) => '(1 2)

Loading…
Cancel
Save