1. Suggestion: declaring multiple interleaved constant sequences
- Posted by petelomax Jun 01, 2014
- 1499 views
One thing I have long bemoaned is the inability to declare multiple corresponding sequence constants in a clear and simple manner. I usually end up doing something like this:
sequence fields_ = {}, descs_ = {} procedure addfield(string field, string desc) fields_ = append(fields_,field) descs_ = append(descs_,desc) end procedure addfield("signature","(Should be \"Mod\\0\")") addfield("size","size (in bytes)") constant fields = fields_, descs = descs_
I think it would be rather nice if we could just do this:
constant {fields,descs} = {"signature"|"(Should be \"Mod\\0\")", "size"|"size (in bytes)"}
The expression on the rhs is just another way of writing
{{"signature","size"}, {"(Should be \"Mod\\0\")","size (in bytes)"}}
Obviously such a change would [have to] apply equally to all general [sequence] expression (and sub-expression) handling, not just constant declarations, and of course there would need to be the same number of '|' present in each and every one of the (multi-)element definitions, if you want to avoid a compilation error that is.
Any thoughts? Has this suggestion already been made, and implemented, and if not, why not?
Pete
2. Re: Suggestion: declaring multiple interleaved constant sequences
- Posted by ArthurCrump Jun 01, 2014
- 1516 views
One thing I have long bemoaned is the inability to declare multiple corresponding sequence constants in a clear and simple manner. I usually end up doing something like this:
sequence fields_ = {}, descs_ = {} procedure addfield(string field, string desc) fields_ = append(fields_,field) descs_ = append(descs_,desc) end procedure addfield("signature","(Should be \"Mod\\0\")") addfield("size","size (in bytes)") constant fields = fields_, descs = descs_
I think it would be rather nice if we could just do this:
constant {fields,descs} = {"signature"|"(Should be \"Mod\\0\")", "size"|"size (in bytes)"}
The expression on the rhs is just another way of writing
{{"signature","size"}, {"(Should be \"Mod\\0\")","size (in bytes)"}}
Obviously such a change would [have to] apply equally to all general [sequence] expression (and sub-expression) handling, not just constant declarations, and of course there would need to be the same number of '|' present in each and every one of the (multi-)element definitions, if you want to avoid a compilation error that is.
Any thoughts? Has this suggestion already been made, and implemented, and if not, why not?
Pete
Would the columnize function help? Perhaps:
include std/sequence.e sequence fields_descs fields_descs = columnize( { {"signature","(Should be \"Mod\\0\")"}, -- etc, {"size","size (in bytes)"} } constant fields = fields_descs[1], descs = fields_descs[2]
Of course, it would be helpful if the multiple assignments in v4.1 could be extended to constants:
constant {fields,descs} = columnize(...etc...) Arthur
3. Re: Suggestion: declaring multiple interleaved constant sequences
- Posted by jimcbrown (admin) Jun 01, 2014
- 1494 views
One thing I have long bemoaned is the inability to declare multiple corresponding sequence constants in a clear and simple manner.
Obviously such a change would [have to] apply equally to all general [sequence] expression (and sub-expression) handling, not just constant declarations,
Any thoughts? Has this suggestion already been made,
Yes. Over ten years ago, now. http://openeuphoria.org/forum/m/62349.wc and http://openeuphoria.org/forum/m/89457.wc
and implemented, and if not, why not?
Parts of it have. http://scm.openeuphoria.org/hg/euphoria/rev/234d97ee08de and http://openeuphoria.org/forum/117157.wc#117157
I don't think it'll work for constants though, but I see no reason to not add that in.
4. Re: Suggestion: declaring multiple interleaved constant sequences
- Posted by DerekParnell (admin) Jun 01, 2014
- 1479 views
What's the issue with coding like this ...
constant fields = {"signature", `(Should be "Mod\0")`}, descs = {"size", "size (in bytes)"}, $
[edit]
Ok... now I get it. I've just spent a little more time reading the actual issue and I see your point now.
5. Re: Suggestion: declaring multiple interleaved constant sequences
- Posted by petelomax Jun 01, 2014
- 1465 views
Would the columnize function help?
It certainly would! Thanks, I consider this suggestion rejected and the problem solved.
Pete
6. Re: Suggestion: declaring multiple interleaved constant sequences
- Posted by mattlewis (admin) Jun 03, 2014
- 1380 views
Any thoughts? Has this suggestion already been made, and implemented, and if not, why not?
I'm not a fan of your syntax. We do have multi-assignment, though not on declaration. To sort of use your example:
sequence fields, descs {fields,descs} = { {"signature", "size}, {"(Should be \"Mod\\0\")", "size (in bytes)"} }
That doesn't really do what you're looking for (though Jim already pointed you to columnize()). It's mostly useful when you have a function that returns multiple values.
Matt
7. Re: Suggestion: declaring multiple interleaved constant sequences
- Posted by jimcbrown (admin) Jun 03, 2014
- 1314 views
(though Jim already pointed you to columnize())
That wasn't me, but ArthurCrump.
8. Re: Suggestion: declaring multiple interleaved constant sequences
- Posted by mattlewis (admin) Jun 03, 2014
- 1331 views
(though Jim already pointed you to columnize())
That wasn't me, but ArthurCrump.
Oops...
Matt
9. Re: Suggestion: declaring multiple interleaved constant sequences
- Posted by petelomax Jun 03, 2014
- 1297 views
Of course, it would be helpful if the multiple assignments in v4.1 could be extended to constants:
constant {fields,descs} = columnize(...etc...)
I just put that into Phix (next release not expected until sometime around autumn 2038 ).
It was ridiculously easy, really, all it needed was an extra parameter on MultipleAssignment() to prohibit subscripts and add the new symtab entries, which might give someone on the OE team an idea.
You (well, I) can also do things like
constant {opcode,{scale,index,base,offset}} = somefunc(mnemonic,var_id)
Admittedly that particular example makes more sense when declaring local variables with this syntax.
integer {opcode,{scale,index,base,offset}} = somefunc(mnemonic,var_id)
Pete
10. Re: Suggestion: declaring multiple interleaved constant sequences
- Posted by ArthurCrump Jul 02, 2014
- 1151 views
Would the columnize function help?
It certainly would! Thanks, I consider this suggestion rejected and the problem solved.
Pete
Now to complicate things.
Suppose a constant name is to be applied to each item/index of a multiple interleaved constant sequences? That could be tricky and could resuurect the original requirement.
For example, in win32lib.ew the colour BrightBlue is represented in three ways:
- The constant BrightBlue
- The string "BrightBlue" and the value:
- rgb{0,0,#FF}
Arthur
11. Re: Suggestion: declaring multiple interleaved constant sequences
- Posted by ArthurCrump Jul 02, 2014
- 1145 views
I intended to add that the constant names could be from an enum
Arthur
12. Re: Suggestion: declaring multiple interleaved constant sequences
- Posted by petelomax Jul 02, 2014
- 1153 views
I'm not sure I follow, well actually I am sure that I don't follow.
I (on Phix) can now happily do this:
function rgb(integer r, integer g, integer b) return r + g * 256 + b * 65536 end function constant Black = rgb(#00, #00, #00), NavyBlue = rgb(#00, #00, #60), Blue = rgb(#00, #00, #80), BrightBlue = rgb(#00, #00, #FF) constant {w32ColorNames,w32ColorValues} = columnize({{"Black",Black}, {"NavyBlue",NavyBlue}, {"Blue",Blue}, {"BrightBlue",BrightBlue}}) for i=1 to length(w32ColorNames) do printf(1,"Color %s Value %08x\n",{w32ColorNames[i],w32ColorValues[i]}) end for
And there is no problem whatsoever with Blue/Black/etc being an enum.
You seem to be suggesting that I should be able to do this (yuk):
constant {w32ColorNames,w32ColorValues} = columnize({{"Black",constant Black=rgb(#00, #00, #00)}, {"NavyBlue",constant NavyBlue= rgb(#00, #00, #60)}, {"Blue",constant Blue= rgb(#00, #00, #80)}, {"BrightBlue",constant BrightBlue= rgb(#00, #00, #FF)}})
or even worse:
constant {w32ColorNames,w32ColorValues} = columnize({{"Black", |enum Black,|}, {"NavyBlue", | NavyBlue,|}, {"Blue", | Blue,|}, {"BrightBlue", | BrightBlue|}})
Confused,
Pete
Edit: Sorry, I also had this strange "construct" in mind:
constant {w32ColorNames,@={w32ColorValues,{Black, |= columnize({{"Black", rgb(#00, #00, #00)}, NavyBlue, | {"NavyBlue", rgb(#00, #00, #60)}, Blue, | {"Blue", rgb(#00, #00, #80)}, BrightBlue}}} | {"BrightBlue", rgb(#00, #00, #FF)}})
not that I'm suggesting that is doable or desireable either.
Edit2: I should also say that if you are on OpenEuphoria,
constant w32Both = columnize(...), w32ColorNames = w32Both[1], w32ColorValues = w32Both[2]
is a fairly minor inconvenience