1. Nested constants

Managed to shoehorn a little featurette in yesterday, took less than an hour.

Instead of

 
                constant WSAEINTR   = 10004, 
                         WSAEACCES  = 10013 
                constant {ERROR_NO, ERROR_NAME, ERROR_SHORT} = columnize( 
                    {{WSAEINTR,         "WSAEINTR",  "Interrupted function call."}, 
                     {WSAEACCES,        "WSAEACCES", "Permission denied."}} 
 

(which gets more and more error prone and tedious as the tables grow in size) you can now define them all together like this:

 
                constant {ERROR_NO, ERROR_NAME, ERROR_SHORT} = columnize( 
                    {{WSAEINTR :=10004, "WSAEINTR",  "Interrupted function call."}, 
                     {WSAEACCES:=10013, "WSAEACCES", "Permission denied."}}) 
 

ie on the second line before the first ',' we see both a definition of WSAEINTR and a reference to it.

Specifically, a ":=" operator at the top level of a {} construct on the rhs of a constant definition is treated as a nested constant definition, and obviously you cannot clash with an existing name or overwrite an existing value.

Right pleased with that, I was.

new topic     » topic index » view message » categorize

2. Re: Nested constants

That is a neat idea, but doesn't ENUM pretty much do the same thing?

enum LEFT, 
     RIGHT, 
     UP, 
     DOWN 
new topic     » goto parent     » topic index » view message » categorize

3. Re: Nested constants

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

?{LEFT,KEYS,NAMES,DESCS} 
-- {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 message » categorize

4. Re: Nested constants

petelomax said...

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

?{LEFT,KEYS,NAMES,DESCS} 
-- {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.

I see what you mean. You want a more advanced way of declaring constants then simple constant and enum. I just thought it could be achieved through enums, but with your method, you need a new way. I mean I do think its a good idea. Here we are still waiting for a new official release of Euphoria, we still keep talking about new features. I just hope the next offical release has some sorta of struct support.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu