From: ·······@arti.vub.ac.be
Subject: package question
Date: 
Message-ID: <l33cxxfff2.fsf@artipc18.vub.ac.be>
Hello all,

I am a bit confused.  Suppose I made myself a package "TABLES" that
implements some table-store and table-lookup functionality (see
e.g. the TABLES.LSP "file" below.)

At top level (common-lisp-user package) I make a table :

   (require "TABLES")
   (setq my-table (tables:make-table))

Suppose now I load some other file defining another package that
defines a function on a table hat stores something in it, which is a
bit the same as the following:

   (in package "SOME-OTHER-PACKAGE")
   (tables:store common-lisp-user::my-table 'a-key 'a-value)
   (in-package "COMMON-LISP-USER")

Now my problem is the following: I cannot lookup 'a-key from within
the common-lisp-user package unless I put some-other-package:: before
it as in:

   (tables:lookup my-table :some-other-package::a-key)

But I find this very annoying.  Is there some way to make sure all
keys used in lookup and store are always in the tables-package or
something so that I could use keys to lookup values without knowing
from within which package they were stored?

How is this normally done?

(I could calculate some hash-key from a key given as argument to store
or lookup but then I would just get another implementation of the
table functionallity and is not considered an answer to my question :-)

Thanks again, joachim.

---------------------
;;; TABLES.LSP file:

(cl:defpackage "TABLES")
(provide 'tables)
(in-package "TABLES")
(export '(make-table lookup store))

(defun make-table () (list '*TABLE*))

(defun lookup (table key) (cdr (assoc key (rest table))))

(defun store (table key value)
  (let ((cc (assoc key (rest table))))
    (if cc (setf (cdr cc) value)        
      (push (cons key value)
            (rest table)))))
--------------------

From: Christopher C. Stacy
Subject: Re: package question
Date: 
Message-ID: <uzo05nt1e.fsf@grant.org>
>>>>> On 15 Apr 2002 12:02:25 +0200, joachim  ("joachim") writes:
 joachim> Is there some way to make sure all keys used in lookup and
 joachim> store are always in the tables-package or something so that
 joachim> I could use keys to lookup values without knowing from
 joachim> within which package they were stored?
 joachim> How is this normally done?

If I understand you correctly, you would like all the table keys 
(which are symbols) that have the same name to be the same.
In other words, no matter what package you're in, looking up
the "FOO" key always gets the same "FOO", and there should be
no distinction between the keys BLORG:FOO and GRONK:FOO.

Three ways come to mind immediately:

1. Require that the users supply keys that are strings, not symbols.
   This means they must write (TABLE:STORE MY-TABLE "FOO" 69)
   The TABLE functions do something like
    (UNLESS (STRINGP KEY)
      (ERROR "The KEY ~S was not a string"))

2. Tell the users of the TABLE functions that they key
   must always be a symbol in the KEYWORD package.
   This means they must write (TABLE:STORE MY-TABLE ':FOO 69)
   The table functions could check this with
    (UNLESS (EQ (SYMBOL-PACKAGE KEY) *KEYWORD-PACKAGE*)
     (ERROR "The KEY ~S was not a keyword"))

3. Change the TABLE:STORE and TABLE:LOOKUP functions so that the
   key is allowed to be either a string or a symbol in any package.
   The key is really a string; the table functions do
    (LET ((REAL-KEY (SYMBOL-NAME KEY))) ...)

   Or, if you want the keys to be symbols for some reason,
   make them really be keywords; the TABLE functions do 
    (LET ((REAL-KEY (INTERN (SYMBOL-NAME KEY) *KEYWORD-PACKAGE*)))

cheers,
Chris
From: Kent M Pitman
Subject: Re: package question
Date: 
Message-ID: <sfwzo05qky6.fsf@shell01.TheWorld.com>
······@grant.org (Christopher C. Stacy) writes:

> 3. Change the TABLE:STORE and TABLE:LOOKUP functions so that the
>    key is allowed to be either a string or a symbol in any package.
>    The key is really a string; the table functions do
>     (LET ((REAL-KEY (SYMBOL-NAME KEY))) ...)
> 
>    Or, if you want the keys to be symbols for some reason,
>    make them really be keywords; the TABLE functions do 
>     (LET ((REAL-KEY (INTERN (SYMBOL-NAME KEY) *KEYWORD-PACKAGE*)))

I wouldn't do the latter of these because INTERN is little more
internally than a table lookup anyway.  So you're doing a table lookup
just to get a key for a table lookup.  That's kind of a waste.  Might
as well just use ASSOC with a STRING= test (or else EQUAL hash
tables).

Also, I'd use STRING instead of SYMBOL-NAME since then anything that
is coerceable to a symbol will work.

 (defun table:lookup (table key-designator)
   (let ((key (string key-designator)))
     (cdr (assoc key (rest table) :test #'string=))))

 (defun table:store (table key-designator value)
   (let ((key (string key-designator)))
     (let ((cell (assoc key (rest table) :test #'string=)))
       (if cell
           (setf (cdr cell) value)
           (let ((new-cell (cons key value)))
             (push new-cell (rest table))
             value)))))