[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Silly question...



>Date: Tue, 4 Aug 92 13:06:13 -0400
>From: Luke Hohmann <hohmann@csmil.umich.edu>
>To: info-macl@cambridge.apple.com
>Subject: Silly question...
>
>
>How do I convert a string to a symbol? i.e., how to I:
>
>(setf foo "window")
>
>and then call a function to convert foo to a symbol so I can use it in
>a call to make-instance?
>
>(make-instance  foo)
>                ^^^
>     --> Won't work. How do I transform foo?
>
>Thanks!
>
>  -- Luke
>

The INTERN command will do the trick - in general, converting
the string to uppercase first is a good idea.

? (setq foo "window")
"window"
? (make-instance (intern (string-upcase foo)))
#<WINDOW "Untitled" #xB6ECE9>


However, I think it's important to point out that using INTERN
in your code is usually a warning sign that you're doing something
wrong. In other words, Common Lisp gives you enough rope to hang
yourself, but that doesn't mean you have to use it.

For example, if you use (setq foo 'window) instead of
(setq foo "window"), you wouldn't have to use INTERN at all.
(make-instance foo) is a heck of a lot more elegant.

Remember, Lisp is really good at manipulating symbols; there's
no need to shy away from using them! Try 'em, you'll like 'em!