unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Lisp help: problem with uninterned symbols
@ 2003-12-12 14:46 Lars Brinkhoff
  0 siblings, 0 replies; 8+ messages in thread
From: Lars Brinkhoff @ 2003-12-12 14:46 UTC (permalink / raw)


Hello,

I have a problem with symbols that aren't interned in any Emacs
obarray.  I intern the symbols in my own tables, but I would still
like to have the symbols as constants in my code, and have them
compare eq as usual.

Here is a simplified version of my code:

    ;;; file my-intern.el
    (defvar symbol-table nil)
    (defun my-intern (string)
      (let ((x (assoc string symbol-table)))
        (if x
            (cdr x)
            (let ((sym (make-symbol string)))
              (push (cons string sym) symbol-table)
              sym))))

Now, to include my-interned symbols as constants, I may use a macro or
eval-when-compile:

  ;;; file foobar.el
  (defun foo () (eval-when-compile (my-intern "foo")))
  (defun bar () (eval-when-compile (my-intern "foo")))

If I load this file and call (eq (foo) (bar)), the result is t as I
like.  However, if I compile the file the result is:

  ;;; file foobar.elc
  [...]
  (defalias 'foo #[nil "\300\207" [#1=#:foo] 1])
  (defalias 'bar #[nil "\300\207" [#1=#:foo] 1])

As you can see, there are two non-eq #:foo in the compiled code, so
(eq (foo) (bar)) will return nil.

If I trick the byte compiler into compiling the functions as one
top-level form, say like

  ;;; file foobar.el
  (let ()
    (defun foo () (eval-when-compile (my-intern "foo")))
    (defun bar () (eval-when-compile (my-intern "foo"))))

the result is:

  ;;; file foobar.elc
  [...]
  (byte-code "\300\301M\210\302\303M\207"
             [foo #[nil "\300\207" [#1=#:foo] 1] bar #[nil "\300\207" [#1#] 1]]
             2)

Here, there's only one #:foo which is referenced a second time with
#1#.  Great, (eq (foo) (bar)) works again.  However, that trick
doesn't work if foo and bar are defined in two separate file.

Any idea how to solve this?

-- 
Lars Brinkhoff,         Services for Unix, Linux, GCC, HTTP
Brinkhoff Consulting    http://www.brinkhoff.se/

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2003-12-12 23:37 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.1638.1071244137.399.help-gnu-emacs@gnu.org>
2003-12-12 15:09 ` Lisp help: problem with uninterned symbols Pascal Bourguignon
2003-12-12 15:40   ` Lars Brinkhoff
     [not found]   ` <mailman.1639.1071247380.399.help-gnu-emacs@gnu.org>
2003-12-12 17:17     ` Pascal Bourguignon
2003-12-12 21:59       ` Lars Brinkhoff
2003-12-12 23:37         ` Lars Brinkhoff
2003-12-12 19:54 ` Stefan Monnier
2003-12-12 20:44   ` Lars Brinkhoff
2003-12-12 14:46 Lars Brinkhoff

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).