Files
lisplings/exercises/14_packages/pkg5.lisp
T

57 lines
1.8 KiB
Common Lisp

;;; PACKAGES 5: INTERNALS, NICKNAMES, SHADOWING
;;;
;;; A single colon reaches only EXPORTED symbols. The double colon
;;; shop::secret-sale reaches the internal ones too. :NICKNAMES
;;; gives a package extra names to call it by. And :SHADOW claims a
;;; name a USEd package would otherwise lend you: FILL below means
;;; POST-OFFICE's own symbol, the claim spelled out in the
;;; defpackage. Without it, plain FILL inside POST-OFFICE reads as
;;; COMMON-LISP's own - the DEFUN below would redefine that for
;;; everyone, and SBCL's package lock refuses, with an error.
(defpackage #:shop
(:use #:common-lisp)
(:nicknames #:store)
(:export #:price))
(in-package #:shop)
(defun price ()
5)
(defun secret-sale ()
2)
(in-package #:common-lisp-user)
;; TODO: The secret sale price SHOP keeps for regulars only.
(defun best-deal ()
(shop:price))
;; TODO: The defpackage below is missing its (:shadow #:fill).
;; Add it, so POST-OFFICE claims FILL up front - without the claim
;; the DEFUN below would redefine COMMON-LISP's own FILL, and
;; SBCL's package lock refuses to load the file at all.
(defpackage #:post-office
(:use #:common-lisp))
(in-package #:post-office)
(defun fill (box)
(list 'stuffed box))
(in-package #:common-lisp-user)
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 5 (store:price))
(check-equal 2 (best-deal))
(check-equal (list 0 0 0) (fill (list 1 2 3) 0))
(check-equal '(post-office::stuffed crate)
(funcall (find-symbol "FILL" '#:post-office) 'crate))
(check (member "FILL" (package-shadowing-symbols '#:post-office)
:test #'string= :key #'symbol-name))