These macros merge control flow and binding. They are an improvement over anaphoric anaphoric macros because they let the developer communicate meaning through naming. As such their use is recommended over their anaphoric counterparts.
(if-let (user (get-user user-id)) (show-dashboard user) (redirect 'login-page))
FOO-LET macros bind one or more variables, and then use those variables as the test form for the corresponding conditional (
WHEN). Multiple variables are combined with
AND. The chosen branch is executed with the bindings in effect. A simple one variable implementation of
IF-LET might look something like:
(defmacro if-let ((var test-form) then-form &optional else-form) `(let ((,var ,test-form)) (if ,var ,then-form ,else-form))) (macroexpand '(if-let (a (getf '(:a 10 :b 20 :c 30) :a)) (format t "A: ~a~%" a) (format t "Not found.~%"))) ; (LET ((A (GETF '(:A 10 :B 20 :C 30) :A))) ; (IF A ; (FORMAT T "A: ~a~%" A) ; (FORMAT T "Not found.~%")))
A version that supports multiple variables is available in the Alexandria library.