Jump to content
dummzeuch

enums with duplicate values

Recommended Posts

Posted (edited)

Did you know that this is possible:

type
  TImageConvertOuputConfigEnum = (
    IMAGE_CONVERT_OUTPUT_CONFIG_IMAGE_ID = $02, //(1 << 1), //**< output pixels points to a ImageId */
    IMAGE_CONVERT_OUTPUT_CONFIG_O0 = $04, //(1 << 2), //**< disable CPU optimizations such as SSSE3 */
    IMAGE_CONVERT_OUTPUT_CONFIG_BAYER_METHOD_1 = $10, //(1 << 4), //**< select bayer decoding method 1 (3x3 interpolation, a.k.a. legacy method) */
    IMAGE_CONVERT_OUTPUT_CONFIG_BAYER_METHOD_2 = $20, //(2 << 4), //**< select bayer decoding method 2 (3x3 median-based interpolation, a.k.a. advanced method) */
    IMAGE_CONVERT_OUTPUT_CONFIG_BAYER_METHOD_3 = $30, //(3 << 4), //**< select bayer decoding method 3 (5x5 gradient-based interpolation) */
    IMAGE_CONVERT_OUTPUT_CONFIG_DEFAULT = IMAGE_CONVERT_OUTPUT_CONFIG_BAYER_METHOD_1,
    IMAGE_CONVERT_OUTPUT_CONFIG_BAYER_ADVANCED = IMAGE_CONVERT_OUTPUT_CONFIG_BAYER_METHOD_2
    );

This is a translation of a C header file. I only now noticed that the enum contains duplicate values which I until now thought was impossible.

 

IMAGE_CONVERT_OUTPUT_CONFIG_DEFAULT is a duplicate of IMAGE_CONVERT_OUTPUT_CONFIG_BAYER_METHOD_1, both having the ordinal value $10, and IMAGE_CONVERT_OUTPUT_CONFIG_BAYER_ADVANCED is a duplicate of IMAGE_CONVERT_OUTPUT_CONFIG_BAYER_METHOD_2, both having the ordinal value $20.

 

The Tokyo Docwiki actually has such an example:

Quote

given the declaration:


type SomeEnum = (e1, e2, e3 = 1);

SomeEnum has only two possible values: Ord(e1) returns 0, Ord(e2) returns 1, and Ord(e3) also returns 1; because e2 and e3 have the same ordinality, they represent the same value.

It compiles with Delphi 2007 so it must have been possible for a long time.

 

"Man wird alt wie 'ne Kuh und lernt immernoch dazu."

-- my mother

Edited by dummzeuch
  • Thanks 1

Share this post


Link to post

If I remember correctly, enums with constants it is a bit tricky with regards to RTTI?

Share this post


Link to post
44 minutes ago, Lars Fosdal said:

If I remember correctly, enums with constants it is a bit tricky with regards to RTTI?

Not tricky, they just have no RTTI 😄 

 

Interesting ability, wasn't aware of it

Share this post


Link to post
2 hours ago, dummzeuch said:

The Tokyo Docwiki actually has such an example:

It compiles with Delphi 2007 so it must have been possible for a long time.

I think it has always been possible. I remember doing quite a bit with assigned values in enums in D2 and D5, and for the same reason: interfacing to third-party C DLLs. I seem to recall having observed it then.

Share this post


Link to post
1 hour ago, Bill Meyer said:

I think it has always been possible. I remember doing quite a bit with assigned values in enums in D2 and D5, and for the same reason: interfacing to third-party C DLLs. I seem to recall having observed it then.

According to my notes, assigning explicit ordinal values to enums has been supported since D6.  I haven't tested it in earlier versions, though.

  • Like 1

Share this post


Link to post
19 minutes ago, Remy Lebeau said:

According to my notes, assigning explicit ordinal values to enums has been supported since D6.  I haven't tested it in earlier versions, though.

An example, then, of memory fade. I must be recalling from D7 days, I suppose.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×