Path: utzoo!attcan!uunet!samsung!usc!apple!cambridge.apple.com!alms From: alms@cambridge.apple.com (Andrew L. M. Shalit) Newsgroups: comp.lang.lisp Subject: Re: Question about INTERN Message-ID: Date: 31 Jan 91 16:40:32 GMT References: <1991Jan29.055536.1523@magnus.ircc.ohio-state.edu> <5783@idunno.Princeton.EDU> <4037@skye.ed.ac.uk> <1991Jan30.225106.26561@Think.COM> Sender: news@cambridge.apple.com Organization: Apple Computer Inc, Cambridge, MA Lines: 49 In-reply-to: barmar@think.com's message of 30 Jan 91 22:51:06 GMT In article <1991Jan30.225106.26561@Think.COM> barmar@think.com (Barry Margolin) writes: Also, a few coding style suggestions: it's usually more correct to use (typep ) than (eq (type-of ) ), for the reasons that have been mentioned in previous responses (it's also more concise to use a specialized type-checking predicate such as SYMBOLP when it's available); use EQ or EQL when you know that you don't need the generality of EQUAL (when I read (EQUAL ...) it forces me to think about why EQUAL was used, whereas EQ is a very simple operation); use SETQ rather than SETF when setting variables (again, seeing the more general operator forces the reader to stop and think, although I suspect many people out there would disagree with me on this particular point, and I sometimes wish the Common Lisp designers had had the guts to get rid of SETQ); use SYMBOL-VALUE rather than EVAL when you know that the argument is a symbol (it's usually more (and never less) efficient, and states your intent more clearly -- in general, EVAL should only be used as a last resort); don't use PROG unless you're using the features of at least two of the special forms it combines (LET, BLOCK, and TAGBODY). All very good suggestions. With all this in mind, here's a suggested rewrite of your functions: (defun get-slot-value (obj slot) "Return the value of the specified slot in obj. Assumes it is a structure whose accessors use the default naming scheme." (when (symbolp obj) (setq obj (symbol-value obj))) (eval `(,(intern (format nil "~a-~a" (type-of obj) slot)) ',obj))) Two more suggestions: replace FORMAT with CONCATENATE. This will ensure that you don't get screwed by *print-case*. Also, you can replace the EVAL with a FUNCALL (going by your "smaller hammer for a smaller job" theory). It might also be a good idea to pass a package argument to INTERN. All these together give us: (defun get-slot-value (obj slot) (when (symbolp obj) (setq obj (symbol-value obj))) (funcall (intern (concatenate 'string (string (type-of obj)) "-" (string slot)) (symbol-package slot)) ;just a guess! obj)) --