anonymous
anonymous

Reputation: 1532

Package agnostic symbol comparison

I have two packages each containing the same symbol:

(make-package "package1")
(make-package "package2")
(intern "SYMBOL" (find-package "PACKAGE1"))
(intern "SYMBOL" (find-package "PACKAGE2"))

and I want to compare them. I need to be able to write an s-expression like this:

(package-agnostic-eq 'package1::symbol 'package2::symbol) ; => t

What would be the most elegant and straightforward way to do this?

In particular I am interested in a build-in operator. Here is the function that I came up with:

(defun package-agnostic-eq (sym1 sym2)
  (string= (symbol-name sym1) (symbol-name sym2)))

Upvotes: 4

Views: 155

Answers (1)

jkiiski
jkiiski

Reputation: 8411

STRING=/STRING-EQUAL takes as its arguments string designators, rather than just strings. That means you can compare symbol names with it too.

CL-USER> (make-package :foo)
#<PACKAGE "FOO">
CL-USER> (make-package :bar)
#<PACKAGE "BAR">
CL-USER> (intern "QUUX" :foo)
FOO::QUUX
NIL
CL-USER> (intern "QUUX" :bar)
BAR::QUUX
NIL
CL-USER> (string= 'foo::quux 'bar::quux)
T

Upvotes: 10

Related Questions