packagelispcommon-lispsymbolsparenscript

Overriding "defun" within a package


I would like to define a macro named "defun" from within a package i am creating and i would like to export it to be used in certain places. There is a library called parenscript that does this in it's package like so,

(export #:defun)

When i try to do this within my own package i get this SBCL error

Lock on package COMMON-LISP violated when defining DEFUN as a macro while in package COMMON-LISP-USER.

How is this done in the parenscript library? I know that you can type the form;

(ps (defun function-name (args) (body)))

I want to be able to do the same but cannot figure out how this is done?


Solution

  • You want to shadow the original symbol from the CL package.

    CL-USER 1 > (defpackage "MY-PACKAGE" (:use "CL"))
    #<The MY-PACKAGE package, 0/16 internal, 0/16 external>
    
    CL-USER 2 > (in-package "MY-PACKAGE")
    #<The MY-PACKAGE package, 0/16 internal, 0/16 external>
    
    MY-PACKAGE 3 > (shadow 'defun)
    T
    
    MY-PACKAGE 4 > (cl:defun defun () :my-defun-returns)
    DEFUN
    
    MY-PACKAGE 5 > (defun)
    :MY-DEFUN-RETURNS
    
    MY-PACKAGE 6 > (export 'defun)
    T