[movitz-devel] Sets of constants

Frode Vatvedt Fjeld ffjeld at common-lisp.net
Wed Mar 21 18:24:36 UTC 2007


In many circumstances there's a need to define sets of constants. For
example, most hardware drivers wants to define names of hardware
registers and values.

My first (and so far only) attempt at a mechanism for this, is in
losp/lib/named-integers.lisp. This scheme doesn't work very well, and
I want to replace it. I'd like to hear if anyone has opinions on this.

My own idea so far is to define for each set of constants a pair of
functions, where the first function maps names (symbols, or keywords
if the definition is supposed to be used from outside the package) to
values, and the second maps values to names. Compile-time expansion of
constant names will be provided.

Here's a scetch in terms of definition and expansion:

  (define-constants ip-protocol ()
    (1 icmp)
    (6 tcp)
    (17 udp))

===>>>

  (progn
   (defun ip-protocol (name)
     (ecase name
       (icmp 1)
       (tcp 2)
       (udp 17)))
   (defun ip-protocol-name (value)
     (case value
       (1 'icmp)
       (2 'tcp)
       (17 'udp))))


Any other ideas, concerns, or experiences?

-- 
Frode Vatvedt Fjeld




More information about the movitz-devel mailing list