I understand that this is trivial with an if, but is there an option, like %S or %s that interpolates nil as no string at all?
Example:
(format "%?.el" nil) ; ".el"
(format "%?.el" "beginner") ; "beginner.el"
I understand that this is trivial with an if, but is there an option, like %S or %s that interpolates nil as no string at all?
Example:
(format "%?.el" nil) ; ".el"
(format "%?.el" "beginner") ; "beginner.el"
Depending on your application, concat might be of use:
(concat "live long " nil "and prosper")
;; => "live long and prosper"
This works because concat acts on sequences, and nil is an empty list.
The special form or is useful here. This macro returns the value of the first argument, unless it's nil in which case it returns the second. So, assuming the variable you want to check is foo, the following will do what you want:
(format "%s.el" (or foo ""))
In some ways it's better than a magic tag since it makes it clear what value should be returned if the argument is nil.
interpolationtag. – The Unfun Cat Mar 01 '15 at 08:36formatindicator for this (useM-x report-emacs-bugfor that). The rest of us have gotten used to usingconcatfor this, sometimes in combination withformat(for other conversions). Or else passing an arg toformatsuch as(if something "foobar" ""), corresponding toformatindicator"%s". – Drew Mar 01 '15 at 15:10(or foo "")which is a decent idiom I guess, but I want to interpolate MANY possibly nil values into this giant regex which makes it a chore. Thanks Drew. – The Unfun Cat Mar 01 '15 at 18:16rxmacro in such a scenario. At minimum make sure you areregexp-quoteing as appropriate), but that aside if you have a large number of maybe-strings in LIST you could always do something like(apply 'format "%s%s%s%s" (mapcar (lambda (x) (or x "")) LIST)). Of course if your format string is literally like"%s%s%s", thenconcatdoes indeed make more sense. – phils Mar 02 '15 at 00:21