This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: procedure-name-symbol to procedure


On 03/02/2013 03:18 AM, Chuah Teong Leong wrote:
Hi, is it possible to construct a symbol and retrieve the procedure
from the interactive environment associated the symbol name?

I have the following code
;;  (if (w-key-event? e)
;;      (begin
;;        (set! last-key 'w)
;;        (register-key-down 'w)
;;        ))
;;  (if (a-key-event? e)
;;      (begin
;;        (set! last-key 'a)
;;        (register-key-down 'a)
;;        ))
;;  (if (s-key-event? e)
;;      (begin
;;        (set! last-key 's)
;;        (register-key-down 's)
;;        ))
;;  (if (d-key-event? e)
;;      (begin
;;        (set! last-key 'd)
;;        (register-key-down 'd)
;;        ))

and I'm probably going to do the same for every key I care about.

The obvious question to me is: Why do you have all these different XX-key-event? procedures? This cries out for something like:

(if (key-event? e)
  (let ((keysym (get-key-symbol e)))
     (if (memq keysym '(w a s e))
       (begin
         (set! last-key keysym)
         (register-key-down keysym))))

In my experience people way over-use eval.  If you feel tempted
to use eval, think three times - it's probably the wrong solution.
Among the other problems (modularity, error-checking, ...) the
most obvious problem is eval is much slower.  (Also, avoid using load;
use require or import instead.)

If you really want to use eval, you need to make sure w-key-event?
is defined in the dynamic environment, and not just the lexical
(compile-time) environment. The most robust way to do this is with
define-variable:

(define-variable w-key-event?
  (lambda (e) ...))

Using plain define will *probably* work, but it is not guaranteed
- it depends on a number of factors, such as whether w-key-event?
is module private,
--
	--Per Bothner
per@bothner.com   http://per.bothner.com/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]