Skip to content

Instantly share code, notes, and snippets.

@notmgsk
Last active February 10, 2019 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notmgsk/e1ca6f104bce29444c53255f3966db50 to your computer and use it in GitHub Desktop.
Save notmgsk/e1ca6f104bce29444c53255f3966db50 to your computer and use it in GitHub Desktop.
(defun blah ()
(let ((fname 'blah))
(returner))
1)
(blah)
(defvar fname nil)
(setf fname 'something)
(defmacro returner ()
`(return-from ,(symbol-value 'fname) nil))
@informatimago
Copy link

informatimago commented Feb 10, 2019

(eval-when (:compile-toplevel :load-toplevel :execute)
  (import 'com.informatimago.common-lisp.lisp-sexp.source-form:parse-body))

(defmacro define-function-with-returner (name (&rest lambda-list) &body body)
  (multiple-value-bind (docstrings declarations body) (parse-body :lambda body)
    `(defun ,name ,lambda-list
       ,docstrings
       ,declarations
       (macrolet ((returner () `(return-from ,',name nil)))
         ,@body))))


(define-function-with-returner blah ()
  (returner)
  1)

(blah)
; --> nil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment