Skip to content

Instantly share code, notes, and snippets.

@guicho271828
Created February 10, 2014 11:34
Show Gist options
  • Save guicho271828/8914342 to your computer and use it in GitHub Desktop.
Save guicho271828/8914342 to your computer and use it in GitHub Desktop.
with-local-package
(in-package :cl-user)
(defpackage :a
(:use :cl))
(in-package :a)
(defun print-square (x)
(print (* x x)))
(in-package :cl-user)
(defmacro with-local-package ((package) &body body)
(let ((*package* (find-package package)))
(let ((*print-escape* nil))
(let ((str (princ-to-string `(progn ,@body))))
(format *error-output* "~&Re-reading part of the code: ~a" str)
(with-input-from-string (s str)
(read s))))))
(defun test ()
(with-local-package (:a)
(print-square 3)))
; compiling (DEFUN TEST ...)
;; Re-reading part of the code: (PROGN (PRINT-SQUARE 3))
;; CL-USER> (test)
;; 9
;; 9
;; CL-USER> (print-square 3)
; in: PRINT-SQUARE 3
; (PRINT-SQUARE 3)
;
; caught STYLE-WARNING:
; undefined function: PRINT-SQUARE
;
; compilation unit finished
; Undefined function:
; PRINT-SQUARE
; caught 1 STYLE-WARNING condition
; Evaluation aborted on #<UNDEFINED-FUNCTION PRINT-SQUARE {1008FDD3F3}>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment