Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package agnostic symbol comparison

Tags:

common-lisp

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)))
like image 410
tsikov Avatar asked Dec 23 '22 23:12

tsikov


1 Answers

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
like image 132
jkiiski Avatar answered Jan 24 '23 13:01

jkiiski