GNU bug report logs -
#49407
Request: Specify default values in `map-let` in Map.el
Previous Next
Reported by: Okam <okamsn <at> protonmail.com>
Date: Sun, 4 Jul 2021 23:10:01 UTC
Severity: wishlist
Fixed in version 30.1
Done: "Basil L. Contovounesios" <contovob <at> tcd.ie>
Bug is archived. No further changes may be made.
Full log
View this message in rfc822 format
On 7/15/21 4:51 AM, Lars Ingebrigtsen wrote:
> Hm... I guess that could be useful. I've added Nicolas to the CCs;
> perhaps he has an opinion here.
If it helps, below is a version of the idea that I am using, specific to
plists:
(defmacro plist-bind (bindings plist &rest body)
"Bind values in PLIST to variables in BINDINGS, surrounding BODY.
- PLIST is a property list.
- BINDINGS is of the form (KEY VAR KEY VAR ...). VAR can
optionally be a list of two elements: a variable name and a
default value, similar to what one would use for expressing
keyword parameters in `cl-defun' or `cl-destructuring-bind'.
The default value is used /only/ when KEY is not found in
PLIST.
- BODY is the same as in `let'.
This macro works the same as `cl-destructuring-bind', except for
the case when keys exist in PLIST that are not listed in
BINDINGS. While `cl-destructuring-bind' would signal an error,
this macro simply ignores them."
(declare (indent 2))
(let ((value-holder (gensym "plist-let-"))
(found-key (gensym "plist-prop-found-")))
`(let* ((,value-holder ,plist)
,@(cl-loop for (key var . _) on bindings by #'cddr
if (consp var)
collect `(,(cl-first var)
;; Use `plist-member' instead of
`plist-get' to
;; allow giving `nil' as an argument
without
;; using the default value.
(if-let ((,found-key (plist-member
,value-holder
,key)))
(cl-second ,found-key)
,(cl-second var)))
else collect `(,var (plist-get ,value-holder
,key))))
,@body)))
This bug report was last modified 2 years and 2 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.