Re: Nested constants

new topic     » goto parent     » topic index » view thread      » older message » newer message

Erm, what I want is the actual constants plus three arrays, which line up by index:

constant LEFT = 331, RIGHT = 333, UP = 328, DOWN = 336, 
         KEYS  = {LEFT,RIGHT,UP,DOWN}, 
         NAMES = {"LEFT","RIGHT","UP","DOWN"}, 
         DESCS = {"move left","shift right","go up","drop down"} 

mainly so I can use idx=find() on the first sequence and then [idx] the other two, and I can't think of any way for enum to do anything beyond the top line.
(I have 61 constants in this particular set already, and from past and painful experience I know that keeping 4 such independent sets in step is no fun at all.)

The only other way I can think of that would achieve anything similar (sanely, and defining everything related side-by-side) might be something like this

sequence keys = {}, names = {}, descs = {} 
function add_key(integer key, string name, desc) 
    keys = append(keys,key) 
    names = append(names,name) 
    descs = append(descs,desc) 
    return key 
end function 
constant LEFT  = add_key(331, "LEFT",  "move left"  ), 
         RIGHT = add_key(333, "RIGHT", "shift right"), 
         UP    = add_key(328, "UP",    "go up"      ), 
         DOWN  = add_key(336, "DOWN",  "drop down"  ), 
         KEYS = keys, NAMES = names, DESCS = descs 

but with this new ":=" syntax you can just do

constant {KEYS, NAMES, DESCS} = columnize({ 
         {LEFT  := 331, "LEFT",  "move left"}, 
         {RIGHT := 333, "RIGHT", "shift right"}, 
         {UP    := 328, "UP",    "go up"}, 
         {DOWN  := 336, "DOWN",  "drop down"}}) 

All three options given would create the same seven constants, such that

-- {331,{331,333,328,336},{"LEFT","RIGHT","UP","DOWN"},{"move left","shift right","go up","drop down"}} 

Does that make more sense?

PS What's that saying? Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.

new topic     » goto parent     » topic index » view thread      » older message » newer message


Quick Links

User menu

Not signed in.

Misc Menu