1. enum options

I really hate to bring this up, but...

in converting the GTK library, we are faced with hundreds of enums.

GTK being written in C, they almost always start with 0.

It would be nice if there were a global option to set enums to start at 0, instead of 1. Something like

export enum base 0 
   stuff, 
   nonsense 

Or is there one already, and I just haven't found it?

And while I'm on the subject, another language I use has named enums, like so:

enum field 
   name, 
   addr, 
   city, 
   state 
 
print(field[3]) => "state" -- nice to be able to do a reverse lookup sometimes 
new topic     » topic index » view message » categorize

2. Re: enum options

irv said...

I really hate to bring this up, but...

in converting the GTK library, we are faced with hundreds of enums.

GTK being written in C, they almost always start with 0.

It would be nice if there were a global option to set enums to start at 0, instead of 1. Something like

export enum base 0 
   stuff, 
   nonsense 

Or is there one already, and I just haven't found it?

And while I'm on the subject, another language I use has named enums, like so:

enum field 
   name, 
   addr, 
   city, 
   state 
 
print(field[3]) => "state" -- nice to be able to do a reverse lookup sometimes 

In fact you can but not as as global setting as an individual setting, like this. enum name = 0, address, city, state

in the above example name = 0 address = 1, city = 2, state = 3

but what would you do with enum starting at zero as those are for use with sequence indexing which in euphoria start at 1?

jacques d.

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

3. Re: enum options

Too bad we can't edit our posts, what I intended to write was:

enum base=0 overrides normal base 1

enum foo,bar,baz foo = 0

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

4. Re: enum options

Oh, and as for what I would do with enums starting with 0?

Use 'em. And save some typing.

GTK, and most other libraries written in C, AFAIK, use base 0 for most everything.

Cutting and pasting hundreds of enums is bound to be more accurate than cutting, pasting, and then going back to change each group to start at 0. Which is what I'm doing right now.

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

5. Re: enum options

irv said...

I really hate to bring this up, but...

in converting the GTK library, we are faced with hundreds of enums.

GTK being written in C, they almost always start with 0.

It would be nice if there were a global option to set enums to start at 0, instead of 1. Something like

export enum base 0 
   stuff, 
   nonsense 

Or is there one already, and I just haven't found it?

I understand. It would be an exceptional thing but useful for those that really need it. How about something like ...

with zeroenum 
enum stuff, nonsense 
without zeroenum 
enum foo,bar 
 
? {stuff, nonsense} -- {0,1} 
? {foo,bar} -- {1,2} 
irv said...

And while I'm on the subject ... to be able to do a reverse lookup

I know how you feel. Such a facility would greatly simplify sections in win32lib.
It is not a good idea, generally applied to every enum, because most do not need it and it would add lot's of size overhead to an application. I suspect in v4.x (after v4.0) we will have named enums and enum properties. Something along the lines of ...

enum XOpts  
    Bordered=2, RoundEdge, Colored=5 
 
? XOpts.count -- 3 
? XOpts.min -- 2 
? XOpts.max -- 5 
? XOpts.names -- {"Bordered", "RoundEdge", "Colored"} 
? XOpts.data -- {2,3,5} 
? Bordered.name -- "Bordered" 

But there a significant syntax issues to be worked out yet.

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

6. Re: enum options

irv said...

Oh, and as for what I would do with enums starting with 0?

Use 'em. And save some typing.

GTK, and most other libraries written in C, AFAIK, use base 0 for most everything.

Cutting and pasting hundreds of enums is bound to be more accurate than cutting, pasting, and then going back to change each group to start at 0. Which is what I'm doing right now.

Just an (bad) idea:
How about replacing each "enum" with "enum dummy=-1," when you need zero based enums?
I don't test it in Eu 4.0 and the manual isn't clear, but, probably, the interpreter will generate duplicate symbol errors. However, it seems to me that a redeclaration of a symbol with the same value could be valid.

-Fernando

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

7. Re: enum options

Derek Parnell said...
with zeroenum 
enum stuff, nonsense 
without zeroenum 
enum foo,bar 

What about this? It would be just as easy as zeroenum and a bit more versatile?

with enum base=0  
enum stuff, nonsense  
  
with enum base=1  
enum foo, bar  
  
with enum base=10  
enum abc, def  

Now, thinking further, not as easy but doable... would this be worth while?

with enum base=10 incrementor 10  
enum ten, twenty, thirty  
  
with enum base=1 incrementor *2  
enum one, two, four, eight, sixteen  

Maybe if we supplied the with enum we could add to it's syntax.

Jeremy

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

8. Re: enum options

Jeremy Cowgar said...
Derek Parnell said...
with zeroenum 
enum stuff, nonsense 
without zeroenum 
enum foo,bar 

What about this? It would be just as easy as zeroenum and a bit more versatile?

with enum base=0  
enum stuff, nonsense  
  
with enum base=1  
enum foo, bar  
  
with enum base=10  
enum abc, def  

Yes, that was my initial thinking but for the purpose of this example I wanted to keep it simple. The base=N is kinda nice but resetting it could be problematic. For example ...

with enum base = 1 
enum foo, bar 
with enum base = 4 
enum qwerty, asdfgh 
. . .  
without enum base -- ??? resets to one 
enum poiuy, trewq 
Jeremy Cowgar said...

Now, thinking further, not as easy but doable... would this be worth while?

with enum base=10 incrementor 10  
enum ten, twenty, thirty  
  
with enum base=1 incrementor *2  
enum one, two, four, eight, sixteen  

Maybe if we supplied the with enum we could add to it's syntax.

Yes it would be worthwhile, especially the *2 for hex-flags used in most UI programming.

I'm worried about scope creep for 4.0 though.

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

9. Re: enum options

Derek Parnell said...

Yes, that was my initial thinking but for the purpose of this example I wanted to keep it simple. The base=N is kinda nice but resetting it could be problematic. For example ...

with enum base = 1 
enum foo, bar 
with enum base = 4 
enum qwerty, asdfgh 
. . .  
without enum base -- ??? resets to one 
enum poiuy, trewq 

I would reset it to the default of 1.

Derek Parnell said...

I'm worried about scope creep for 4.0 though.

Yes, this doesn't have to be in 4.0. I'm just thinking while the subject is alive.

Jeremy

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

10. Re: enum options

Khm. I just do not understand this discussion. Why do you want to introduce a new keyword ("base") and even more confuse newbies with more complicated syntax ("with base i") if all you need is something that you already have?

enum stuff=0, 
    nonsense 

Please do not tell me that you want to "save some typing".

Regards,

Salix {:-.

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

11. Re: enum options

Salix said...

Khm. I just do not understand this discussion. Why do you want to introduce a new keyword ("base") and even more confuse newbies with more complicated syntax ("with base i") if all you need is something that you already have?

enum stuff=0, 
    nonsense 

Please do not tell me that you want to "save some typing".

Salix, I agree with you. I do like the increment though smile

Jeremy

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

12. Re: enum options

Jeremy Cowgar said...
Derek Parnell said...

Yes, that was my initial thinking but for the purpose of this example I wanted to keep it simple. The base=N is kinda nice but resetting it could be problematic. For example ...

with enum base = 1 
enum foo, bar 
with enum base = 4 
enum qwerty, asdfgh 
. . .  
without enum base -- ??? resets to one 
enum poiuy, trewq 

I would reset it to the default of 1.

Derek Parnell said...

I'm worried about scope creep for 4.0 though.

Yes, this doesn't have to be in 4.0. I'm just thinking while the subject is alive.

Jeremy

There is a little known include file in win32lib that is called series.e. It implements all of the above and more. Why not simply make it a standard include? No need to ask for a permission.

Doing this would make a difference between builtin enums as were implemented and series. But I don't think it would be terribly difficult to put this code inside the interpreter proper. ?ot in 4.0 alpha, there is some urgency to release something. And there are issues to blend the syntax into known enums. I had tried to push for named enums, but....

How about this?

enum [ ( [from start_value] [to end_value]  
         [by additive_value] | [grow multiply_factor] | [using routine_id]  
         [label "fields"] ) ] 
 foo, 
 bar, 
... 

The brackets only show optional components, and are not to be written.

The parentheses mark the limits of the option list as opposed to the member list. I'd have nothing against using { } instead. Just some pair of delimiters currently not expected after enum.

In keeping with other labels currently in the language, the enum identifier is a string. However, contrary to current other labels, it may need to hold a valid identifier so as to be able to query properties of the enum. This restriction could be removed if we implement the query as enum_meta("fields", ENUM_FROM). You get the idea.

CChris

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

13. Re: enum options

CChris said...

There is a little known include file in win32lib that is called series.e. It implements all of the above and more. Why not simply make it a standard include? No need to ask for a permission.

Doing this would make a difference between builtin enums as were implemented and series. But I don't think it would be terribly difficult to put this code inside the interpreter proper. ?ot in 4.0 alpha, there is some urgency to release something. And there are issues to blend the syntax into known enums. I had tried to push for named enums, but....

How about this?

enum [ ( [from start_value] [to end_value]  
         [by additive_value] | [grow multiply_factor] | [using routine_id]  
         [label "fields"] ) ] 
 foo, 
 bar, 
... 

The brackets only show optional components, and are not to be written.

The parentheses mark the limits of the option list as opposed to the member list. I'd have nothing against using { } instead. Just some pair of delimiters currently not expected after enum.

In keeping with other labels currently in the language, the enum identifier is a string. However, contrary to current other labels, it may need to hold a valid identifier so as to be able to query properties of the enum. This restriction could be removed if we implement the query as enum_meta("fields", ENUM_FROM). You get the idea.

CChris

Is your grow clause for using for flags we see so often in C interfaces where you can specify values by combining constants with binary or?

I don't think label is consistent with the usage in the rest of the language though. Enum isn't flow control. An enum is a special type. Is "fields" the name of the set of constants?

Now, "using routine_id". Could you explain what this means?

I would go with an example:

type enum linux_mem_access_type from 1 to 8 grow 2 
-- check the C docs before using this. :) 
 READ, 
 WRITE, 
 EXEC, 
 NONE = 0 
end type 

Now the above would not only define the constants but also the type,

Look at this header: procedure mem_linux_reprotect( atom addr, linux_mem_access_type pr )

Now pr is a new protection for linux_mem_access and the interpreter make sure that pr is some combination of READ, WRITE, EXEC or 0. Essentialy the procedural defniition of linux_mem_access_type() is:

return pr&(READ|WRITE|EXEC) == pr

more generally it would be

type enum typename [ ( [from start_value] [to end_value]  
         [by additive_value] | [grow multiply_factor] | [using routine_id]  
         ) ] 
 foo, 
 bar, 
... 
end type 

Finally, Often it would be useful but it nearly is always absent for the user, I.E. the programmer, to either through a bug report or on the screen to see the identifier's name rather than the numeric value. An option to include that would be useful for debugging purposes. This is normally done in C by using the macro processor

Shawn Pringle

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

14. Re: enum options

Shawn Pringle said...
CChris said...

There is a little known include file in win32lib that is called series.e. It implements all of the above and more. Why not simply make it a standard include? No need to ask for a permission.

Doing this would make a difference between builtin enums as were implemented and series. But I don't think it would be terribly difficult to put this code inside the interpreter proper. ?ot in 4.0 alpha, there is some urgency to release something. And there are issues to blend the syntax into known enums. I had tried to push for named enums, but....

How about this?

enum [ ( [from start_value] [to end_value]  
         [by additive_value] | [grow multiply_factor] | [using routine_id]  
         [label "fields"] ) ] 
 foo, 
 bar, 
... 

The brackets only show optional components, and are not to be written.

The parentheses mark the limits of the option list as opposed to the member list. I'd have nothing against using { } instead. Just some pair of delimiters currently not expected after enum.

In keeping with other labels currently in the language, the enum identifier is a string. However, contrary to current other labels, it may need to hold a valid identifier so as to be able to query properties of the enum. This restriction could be removed if we implement the query as enum_meta("fields", ENUM_FROM). You get the idea.

CChris

Is your grow clause for using for flags we see so often in C interfaces where you can specify values by combining constants with binary or?

Yes, and the permission example you give later is simply correct.

Shawn Pringle said...

I don't think label is consistent with the usage in the rest of the language though. Enum isn't flow control. An enum is a special type. Is "fields" the name of the set of constants?

enum is a for loop with a twist applied to generating constants. In another language, you could say %FOR %I=1 %TO 4;%DO; %LET IDENT=names[&I]; &IDENT = &I,; %END; the last trailing comma needing extra care, left out.

That's why I think presenting an enum as as close as possible as a for loop makes sense. So it can have a label, which is the name of the enum. And if you want to query the properties of an enum, you need a name for it. I find the latter obvious, but some other people never got it.

Shawn Pringle said...

Now, "using routine_id". Could you explain what this means?

Use a custom function to define what the next value in the enum is. Adding a constant or multiplying by a constant are frequent cases, but you can use a generic iterator. The latter takes current value and returns next value.

function next_value(integer p) return p*p end function

enum (using routine_id(next_value")) three = 3, three_squared, 9 three_power_4, 81 three_power_8, 6561 unrelated_constant = 123

Admittedly, the "from" clause could be removed, I left it in for the purpose of uniform coding. Everything in the ( ) is optional. Overriding with an explicit = is still possible. You can always insert values that didn't fit in the pattern at the end. The "to" part may not be needed either - I'm less sure of this.

The above would be implemented as

constant  
   three = 3,  
   three_squared = call_func(rid,{three}),  
   three_power_4 = call_func(rid, {three_squared}), 
-- ... 
Shawn Pringle said...

I would go with an example:

type enum linux_mem_access_type from 1 to 8 grow 2 
-- check the C docs before using this. :) 
 READ, 
 WRITE, 
 EXEC, 
 NONE = 0 
end type 

Now the above would not only define the constants but also the type,

I think one of the meta properties of an enum could be the whole set of values it takes, so this syntax would not be necessary.
if find(something,enum_meta("permissions", META_LIST)) then -- ok
assuming you used a label "permissions" clause.

Shawn Pringle said...

Look at this header: procedure mem_linux_reprotect( atom addr, linux_mem_access_type pr )

Now pr is a new protection for linux_mem_access and the interpreter make sure that pr is some combination of READ, WRITE, EXEC or 0. Essentialy the procedural defniition of linux_mem_access_type() is:

return pr&(READ|WRITE|EXEC) == pr

more generally it would be

type enum typename [ ( [from start_value] [to end_value]  
         [by additive_value] | [grow multiply_factor] | [using routine_id]  
         ) ] 
 foo, 
 bar, 
... 
end type 

Why not use what we have?

type permissions_mask(atom x) return x = and_bits(x, or_all(enum_meta("permissions",META_LIST))) end type

Ok, or_all() is not yet in std/math.e, but should be there.

Shawn Pringle said...

Finally, Often it would be useful but it nearly is always absent for the user, I.E. the programmer, to either through a bug report or on the screen to see the identifier's name rather than the numeric value. An option to include that would be useful for debugging purposes. This is normally done in C by using the macro processor

Shawn Pringle

This means that x = EXEC wouldn't simply store 4 in x, but store something like {"permissions","EXEC",4}, in an adequate way?

Looks good at a glance. trace() could show EXEC instead of 4. Dealing with Windows messages would be so much easier. Nice.
What should trace(1) display after x = or_bits(x, READ)? I think it will be hopelessly outsmarted if asked to obviously answer READ | EXEC.

I don't know what trace() should do if the enum had no name?

This may not be impossible, butlooks very, very tricky.

CChris

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

15. Re: enum options

enum should be perform in the same way that it is used in C.

Why don't you go to a C compiler source code and see how it is done

in that code.

If it was done this way then translating to compiled C code would be straight forward and new users who use C would not be confused.

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

16. Re: enum options

Bernie said...

enum should be perform in the same way that it is used in C.

Why don't you go to a C compiler source code and see how it is done in that code.

The last thing I want to do is to go through the source code for a C-compiler. I'm not sure what purpose this would serve, in any case.

Bernie said...

If it was done this way then translating to compiled C code would be straight forward...

I can't imagine how it would make anything easier when translating to C code.

Bernie said...

...and new users who use C would not be confused.

This suggestion assumes that the most common usage of enums will be for wrapping C code. I doubt this to be the case. And I care more for making euphoria easier for euphoria programmers than for theoretical users coming from another language. They know that they're using a different language. Since we're actually programming in euphoria, we should follow euphoria conventions, and starting at 1 is much more useful. It's really not that difficult to add in the "= 0" to the first enum, if that's the requirement.

Further, I think that the "with base=n" will lead to difficult to maintain code. I think that if you need some weirdness, it's better to explicitly declare it. If you're creating a set of bit flags, don't use enums. Use constants. It will be much clearer for everyone reading the code.

Matt

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

17. Re: enum options

Matt:

What I was trying to point out was that the way being discussed was tto complicated and confusing for a user.

Hers is a simple idea.

The reserved word enum would be considered as a constant and have 2 forms.

enum or global enum depending on its scope.

example :

enum 1 <<== this value following enum is the increment or decrement value. foo = 0, <<== sets the starting point apple, banna, <<== each coma causes an increment orange, barr, beer, <<== this is 5 beef = 23, <<== this changes our count value telephone, <<== this is 24 bicycle; <<== semicolon ends the enumeration

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

18. Re: enum options

Matt:

I hope this doesn't wrap

What I was trying to point out was that the way being discussed was too complicated and confusing for a user.

Heres is a simple idea.

The reserved word enum would be considered as a constant and have 2 forms.

enum or global enum depending on its scope.

example :

enum 1 <<== this value following enum is the increment or decrement value.
foo = 0, <<== sets the starting point
apple,
banna, <<== each coma causes an increment
orange,
barr,
beer, <<== this is 5
beef = 23, <<== this changes our count value
telephone, <<== this is 24
bicycle; <<== semicolon ends the enumeration

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

19. Re: enum options

Bernie said...

Matt:

What I was trying to point out was that the way being discussed was tto complicated and confusing for a user.

Hers is a simple idea.

The reserved word enum would be considered as a constant and have 2 forms.

enum or global enum depending on its scope.

You've left out export. :) But I agree that the discussion was going off into places it probably shouldn't.

Bernie said...

example :

enum 1 <<== this value following enum is the increment or decrement value.
foo = 0, <<== sets the starting point
apple,
banna, <<== each coma causes an increment
orange,
barr,
beer, <<== this is 5
beef = 23, <<== this changes our count value
telephone, <<== this is 24
bicycle; <<== semicolon ends the enumeration

I think CChris' possible syntax was slightly better (using familiar words such as by).

Matt

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

20. Re: enum options

Matt Lewis said...
Bernie said...

enum should be perform in the same way that it is used in C.

Why don't you go to a C compiler source code and see how it is done in that code.

The last thing I want to do is to go through the source code for a C-compiler. I'm not sure what purpose this would serve, in any case.

Bernie said...

If it was done this way then translating to compiled C code would be straight forward...

I can't imagine how it would make anything easier when translating to C code.

Bernie said...

...and new users who use C would not be confused.

This suggestion assumes that the most common usage of enums will be for wrapping C code. I doubt this to be the case. And I care more for making euphoria easier for euphoria programmers than for theoretical users coming from another language. They know that they're using a different language. Since we're actually programming in euphoria, we should follow euphoria conventions, and starting at 1 is much more useful. It's really not that difficult to add in the "= 0" to the first enum, if that's the requirement.

If you only need to tweak one or two enums in a program, this approuach is correct. If you have scores of them, it is not correct at all. At least terrbly unpractical.

Matt Lewis said...

Further, I think that the "with base=n" will lead to difficult to maintain code. I think that if you need some weirdness, it's better to explicitly declare it.

Isn't "with base n" explicit? I don't understand. It will also help Basic users, who have an Options Base n directive as well.

Matt Lewis said...

If you're creating a set of bit flags, don't use enums.

Why?

Matt Lewis said...

Use constants. It will be much clearer for everyone reading the code.

Matt

And much more tedious to code. I cannot see how the example Shawn posted is not clear. Could you elaborate? By the ay, nothing prevents you from adding comment marks with an explicit value to the enum.

Comments have a precise purpose in the (any) language: tell reader things a machine doesn't need to know, or in a different way. The language constructs do not need to duplicate one another's functionality, and this applies to coments just as well.

CChris

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

21. Re: enum options

CChris said...
Shawn Pringle said...

Look at this header: procedure mem_linux_reprotect( atom addr, linux_mem_access_type pr )

Now pr is a new protection for linux_mem_access and the interpreter make sure that pr is some combination of READ, WRITE, EXEC or 0. Essentialy the procedural defniition of linux_mem_access_type() is:

return pr&(READ|WRITE|EXEC) == pr

more generally it would be

type enum typename [ ( [from start_value] [to end_value]  
         [by additive_value] | [grow multiply_factor] | [using routine_id]  
         ) ] 
 foo, 
 bar, 
... 
end type 

Why not use what we have?

type permissions_mask(atom x) return x = and_bits(x, or_all(enum_meta("permissions",META_LIST))) end type

Ok, or_all() is not yet in std/math.e, but should be there.

So enum_meta returns all possible values then. It seems to be something that would be added everytime you used the grow statement. Why not make it automatic?

When you use the by clause the routine would be:

type other_enum( atom x ) return find( x, enum_meta("sequential_enum",META_LIST) ) end type

CChris said...
Shawn Pringle said...

Finally, Often it would be useful but it nearly is always absent for the user, I.E. the programmer, to either through a bug report or on the screen to see the identifier's name rather than the numeric value. An option to include that would be useful for debugging purposes. This is normally done in C by using the macro processor

Shawn Pringle

This means that x = EXEC wouldn't simply store 4 in x, but store something like {"permissions","EXEC",4}, in an adequate way?

Looks good at a glance. trace() could show EXEC instead of 4. Dealing with Windows messages would be so much easier. Nice.
What should trace(1) display after x = or_bits(x, READ)? I think it will be hopelessly outsmarted if asked to obviously answer READ | EXEC.

No. Create a butltin: enum_name( "permissions", EXEC ) which would return "EXEC", the string. Then when you got an error. You could actually give the user a string. You would supply which enum was used to define EXEC.

What about enumerated types with the grow clause? It's easy, you test for each value with and in the binary case and you concatonate with pipes. In the string.

So enum_name( "permissions", EXEC|WRITE ) would yeild "EXEC|WRITE".

CChris said...

I don't know what trace() should do if the enum had no name?

This may not be impossible, butlooks very, very tricky.

CChris

A program cannot tell the diference between NONE in the enum of permissions and the constant NULL, or another value which is equal to 0. The interpreter would have to keep track of which enum a constant came from and for combined flags it would be even trickier.

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

22. Re: enum options

CChris said...
Matt Lewis said...

This suggestion assumes that the most common usage of enums will be for wrapping C code. I doubt this to be the case. And I care more for making euphoria easier for euphoria programmers than for theoretical users coming from another language. They know that they're using a different language. Since we're actually programming in euphoria, we should follow euphoria conventions, and starting at 1 is much more useful. It's really not that difficult to add in the "= 0" to the first enum, if that's the requirement.

If you only need to tweak one or two enums in a program, this approuach is correct. If you have scores of them, it is not correct at all. At least terrbly unpractical.

It's not as bad as you say. Most of the time, you can cut and paste from C source, and do some find and replace. I think I've done as much of this as anyone has, and it's somewhat of a pain, but the reality is that you're most commonly dealing with simple macros, not enums, so you have to go back and add equals signs, commas and change from C-style hex (0xdeadbeef) to euphoria style (#DEADBEEF). Has anyone ever actually had to convert scores of enums? Even so, it's the addition of a single = 0 to each one. And, it's a one-time cost.

Bottom line, there is no practical way to convert large amounts of code from another language.

CChris said...
Matt Lewis said...

Further, I think that the "with base=n" will lead to difficult to maintain code. I think that if you need some weirdness, it's better to explicitly declare it.

Isn't "with base n" explicit? I don't understand. It will also help Basic users, who have an Options Base n directive as well.

It's explicit wherever that statement is, which is not necessarily where the enum is declared.

CChris said...
Matt Lewis said...

If you're creating a set of bit flags, don't use enums. Use constants. It will be much clearer for everyone reading the code.

Why?

Because then it's more obvious what they're for. That's just my opinion. The values really stand out.

CChris said...

And much more tedious to code. I cannot see how the example Shawn posted is not clear. Could you elaborate? By the ay, nothing prevents you from adding comment marks with an explicit value to the enum.

Comments have a precise purpose in the (any) language: tell reader things a machine doesn't need to know, or in a different way. The language constructs do not need to duplicate one another's functionality, and this applies to coments just as well.

It's adding complication to something for basically zero benefit. If I'm going to put comments in, why would I use an enum? The only place I could see using a comment for an enum value is within a long list, in order to be able to figure out which value was which, but even then, it would only be every 5 or 10, or something similar. Doing this for bit flags would be as inane as:

    i +=1 -- increase i by one 

Matt

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

23. Re: enum options

Shawn Pringle said...
CChris said...
Shawn Pringle said...

Look at this header: procedure mem_linux_reprotect( atom addr, linux_mem_access_type pr )

Now pr is a new protection for linux_mem_access and the interpreter make sure that pr is some combination of READ, WRITE, EXEC or 0. Essentialy the procedural defniition of linux_mem_access_type() is:

return pr&(READ|WRITE|EXEC) == pr

more generally it would be

type enum typename [ ( [from start_value] [to end_value]  
         [by additive_value] | [grow multiply_factor] | [using routine_id]  
         ) ] 
 foo, 
 bar, 
... 
end type 

Why not use what we have?

type permissions_mask(atom x) return x = and_bits(x, or_all(enum_meta("permissions",META_LIST))) end type

Ok, or_all() is not yet in std/math.e, but should be there.

So enum_meta returns all possible values then. It seems to be something that would be added everytime you used the grow statement. Why not make it automatic?

When you use the by clause the routine would be:

type other_enum( atom x ) return find( x, enum_meta("sequential_enum",META_LIST) ) end type

Because, while the grow 2 clause is a candidate for frequent use, grow 1.035 or grow 3 have their uses too. A systematic or_all() woudn't make sense there, or would fail.

I am not against
define_type(string enum_name, string type_name = make_identifier(enum_name)) though.

Shawn Pringle said...
CChris said...
Shawn Pringle said...

Finally, Often it would be useful but it nearly is always absent for the user, I.E. the programmer, to either through a bug report or on the screen to see the identifier's name rather than the numeric value. An option to include that would be useful for debugging purposes. This is normally done in C by using the macro processor

Shawn Pringle

This means that x = EXEC wouldn't simply store 4 in x, but store something like {"permissions","EXEC",4}, in an adequate way?

Looks good at a glance. trace() could show EXEC instead of 4. Dealing with Windows messages would be so much easier. Nice.
What should trace(1) display after x = or_bits(x, READ)? I think it will be hopelessly outsmarted if asked to obviously answer READ | EXEC.

No. Create a butltin: enum_name( "permissions", EXEC ) which would return "EXEC", the string. Then when you got an error. You could actually give the user a string. You would supply which enum was used to define EXEC.

That's the part I don't like. How would we do that?

Shawn Pringle said...

What about enumerated types with the grow clause? It's easy, you test for each value with and in the binary case and you concatonate with pipes. In the string.

So enum_name( "permissions", EXEC|WRITE ) would yeild "EXEC|WRITE".

This one isn't an issue indeed.

CChris said...

I don't know what trace() should do if the enum had no name?

This may not be impossible, butlooks very, very tricky.

CChris

A program cannot tell the diference between NONE in the enum of permissions and the constant NULL, or another value which is equal to 0. The interpreter would have to keep track of which enum a constant came from and for combined flags it would be even trickier.

[/quote]

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

24. Re: enum options

<snipped the practicality part, Irv is more qualified to comment on it/>

Matt Lewis said...
CChris said...
Matt Lewis said...

Further, I think that the "with base=n" will lead to difficult to maintain code. I think that if you need some weirdness, it's better to explicitly declare it.

Isn't "with base n" explicit? I don't understand. It will also help Basic users, who have an Options Base n directive as well.

It's explicit wherever that statement is, which is not necessarily where the enum is declared.

Another scale issue.
If you have a couple enums to tweak, it is not neeeded. If you have a lot of them, then chances are that all of them are to be tweaked. And then the need to be explicit disappears, as all enums would be treated the same.

Don't get me wrong. I prefer the (from start_value ...) form. But your point was I think not much justified.

Matt Lewis said...
CChris said...
Matt Lewis said...

If you're creating a set of bit flags, don't use enums. Use constants. It will be much clearer for everyone reading the code.

Why?

Because then it's more obvious what they're for. That's just my opinion. The values really stand out.

If the explicit values matter, then indeed use plain constants, whether for flags or anything else. If they don't, use enums for flags or anything else. Relevant names are often the best way out of the problem. They are part of the commenting.

Matt Lewis said...
CChris said...

And much more tedious to code. I cannot see how the example Shawn posted is not clear. Could you elaborate? By the way, nothing prevents you from adding comment marks with an explicit value to the enum.

Comments have a precise purpose in the (any) language: tell reader things a machine doesn't need to know, or in a different way. The language constructs do not need to duplicate one another's functionality, and this applies to coments just as well.

It's adding complication to something for basically zero benefit. If I'm going to put comments in, why would I use an enum? The only place I could see using a comment for an enum value is within a long list, in order to be able to figure out which value was which, but even then, it would only be every 5 or 10, or something similar. Doing this for bit flags would be as inane as:

    i +=1 -- increase i by one 

Matt

Since there is less code to convey the same semantics, it brings more simplicity rather than complexity. I agree with your last point about comments. But I see it buttressing my position more than yours.

CChris

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

25. Re: enum options

CChris said...
Matt Lewis said...
CChris said...
Matt Lewis said...

Further, I think that the "with base=n" will lead to difficult to maintain code. I think that if you need some weirdness, it's better to explicitly declare it.

Isn't "with base n" explicit? I don't understand. It will also help Basic users, who have an Options Base n directive as well.

It's explicit wherever that statement is, which is not necessarily where the enum is declared.

Another scale issue.
If you have a couple enums to tweak, it is not neeeded. If you have a lot of them, then chances are that all of them are to be tweaked. And then the need to be explicit disappears, as all enums would be treated the same.

Don't get me wrong. I prefer the (from start_value ...) form. But your point was I think not much justified.

I don't know how to respond to this. The justification was that you can modify the way the code is interpreted by something drastically removed from the code itself. And there's no indication that this is taking place. What more justification should there be?

Yes, you would have a slight savings in effort if you didn't have to go through and adjust all of the enums. But again, this is a one time cost. You have to remember somehow that you're using a modified enum without any clue nearby in the source. What about this isn't clear?

I don't know, I guess we just live on different planets. :/

Mat

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

26. Re: enum options

irv said...

Oh, and as for what I would do with enums starting with 0?

Use 'em. And save some typing.

GTK, and most other libraries written in C, AFAIK, use base 0 for most everything.

Cutting and pasting hundreds of enums is bound to be more accurate than cutting, pasting, and then going back to change each group to start at 0. Which is what I'm doing right now.

Irv, if you do alot of C library binding, you should rather write an euphoria script to parse C .h files to extract all #define and convert to euphoria code. Personnaly it's what I have done. windows sdk has thousands of #define, it's quite a good investment to write such a parser.

jacques d.

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

27. Re: enum options

Just thinking, what about enum [by <expression>] Like..

enum by * 2 
   something = 0, 
   otherthing, 
   etc 
end enum 
new topic     » goto parent     » topic index » view message » categorize

28. Re: enum options

euphoria 4.0 alpha is not out yet adn we are discussing complicated enum. I agree with Matt on that, leave it as it is defined in 4.0.

jacques d.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu