Recent

Author Topic: [SOLVED] Enumerated constant types using existing ordinal types...  (Read 1670 times)

GypsyPrince

  • Guest
Re: Enumerated constant types using existing ordinal types...
« Reply #15 on: April 08, 2020, 01:10:03 am »
yep

winni

  • Hero Member
  • *****
  • Posts: 1510
Re: Enumerated constant types using existing ordinal types...
« Reply #16 on: April 08, 2020, 01:24:32 am »
Yep?????

1=0

?????

GypsyPrince

  • Guest
Re: Enumerated constant types using existing ordinal types...
« Reply #17 on: April 08, 2020, 01:32:12 am »
I had the numeric index values wrong.

Code: Pascal  [Select][+][-]
  1. type
  2.   TCalDays= (Sunday=0, Monday=1, Tuesday=2, Wednesday=3, Thursday=4, Friday=5, Saturday=6);

My point stands...
« Last Edit: April 08, 2020, 07:49:23 am by GypsyPrince »

GypsyPrince

  • Guest
Re: Enumerated constant types using existing ordinal types...
« Reply #18 on: April 08, 2020, 05:39:45 am »
@winni
@mercurhyo

While it is still 1 step more required than what I would prefer, the GetEnumName() and GetEnumValue() functions are 1 step less than would be required using the WriteStr() function (they eliminate the need for use of a variable), and I think I can use them to work with current enumerated type structure as follows.

Code: Pascal  [Select][+][-]
  1. type
  2.   TCalDays = (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);
  3.  
  4. procedure TMainForm.FormCreate(Sender: TObject);
  5. begin
  6.     ShowMessage(GetEnumName(TypeInfo(TCalDays), Ord(Tuesday)));         //Displays "Tuesday"...
  7.  
  8.     ShowMessage(InttoStr(GetEnumValue(TypeInfo(TCalDays), 'Tuesday'))); //Displays "2"...
  9. end;

Found this at:

https://forum.lazarus.freepascal.org/index.php?topic=33819.0

Many thanks to both of you gentlemen for your assistance!
« Last Edit: April 08, 2020, 07:30:34 am by GypsyPrince »

PascalDragon

  • Hero Member
  • *****
  • Posts: 1744
  • Compiler Developer
Re: Enumerated constant types using existing ordinal types...
« Reply #19 on: April 08, 2020, 10:47:11 am »
While it is still 1 step more required than what I would prefer, the GetEnumName() and GetEnumValue() functions are 1 step less than would be required using the WriteStr() function (they eliminate the need for use of a variable), and I think I can use them to work with current enumerated type structure as follows.

But if you look at the code you'll see that using GetEnumName is less efficient than using WriteStr (use of the former results in an implicit exception frame, and GetEnumValue needs to do pointer adjustments to get the names after a check whether it's dealing with Booleans while the later directly passes the conversion table to the writer function and there's no implicit exception frame).

Also you can always create yourself a type helper:

Code: Pascal  [Select][+][-]
  1. // interface section
  2. type
  3.   TCalDaysHelper = type helper for TCalDays
  4.     function ToString: String; inline;
  5.   end;
  6.  
  7. // implementation section
  8. function TCalDaysHelper.ToString: String;
  9. begin
  10.   WriteStr(Result, Self);
  11. end;
  12.  
  13. // somewhere else:
  14. Tuesday.ToString
  15. // or
  16. caldaysvar.ToString

GypsyPrince

  • Guest
Re: [Solved] Enumerated constant types using existing ordinal types...
« Reply #20 on: April 08, 2020, 06:28:32 pm »
rvk
Quote
Hero Member
*****

Posts: 3950

Re: Getting a string representation of an enum value
« Reply #4 on: August 25, 2016, 04:20:17 pm »
Quote from: fatmonk on August 25, 2016, 04:19:36 pm
I did look at WriteStr() but missed that the result gets sent to the first argument...
I also edited my post to include the GetEnumName-example which is what WriteStr() uses internally.

The line:

Quote
I also edited my post to include the GetEnumName-example which is what WriteStr() uses internally.

from that post in the link I provided earlier is the only reason I went with the GetEnumName() function over the WriteStr() procedure.

I wish there was a way to verify whether that particular statement is true.

JdeHaan

  • Jr. Member
  • **
  • Posts: 57
Re: [Solved] Enumerated constant types using existing ordinal types...
« Reply #21 on: April 08, 2020, 06:44:44 pm »
on my mac with Catalina this prints the text 'Tuesday'.

Code: Pascal  [Select][+][-]
  1. program enumwrite;
  2.  
  3. type
  4.   TCalDays= (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);
  5.  
  6. begin
  7.   writeln(TCalDays.Tuesday); // prints 'Tuesday'
  8. end.
  9.  
  10.  


Lazarus 2.1.0 r62775 FPC 3.3.1 x86_64-darwin-cocoa (beta)

GypsyPrince

  • Guest
Re: [Solved] Enumerated constant types using existing ordinal types...
« Reply #22 on: April 08, 2020, 08:36:05 pm »
Code: Pascal  [Select][+][-]
  1. Type ControlCharacters = (
  2.     CTL_AKNWLDGMNT : Char = #6, //Control character; acknowledgment.
  3.     CTL_BACKSPACE : Char = #8,  //Control character; backspace.
  4.     CTL_BELL : Char = #7,       //Control character; bell ("ding") sound.
  5.     CTL_CANCEL : Char = #24;    //Control character; cancel.
  6.     CTL_CRGRTRN : Char = #13,   //Control character; carriage return.
  7.     CTL_DATALINK : Char = #16   //Control character; data link.
  8.   );
  9.  
  10.  
  11.   Type DrawingCharacters = (
  12.     DWG_ALT179 : String = '│', //Drawing character; line - single vertical. Equal to {Alt+179}.
  13.     DWG_ALT180 : String = '┤', //Drawing character; right coupler - single vertical/single horizontal. Equal to {Alt+180}.
  14.     DWG_ALT181 : String = '╡', //Drawing character; right coupler - single vertical/double horizontal. Equal to {Alt+181}.
  15.     DWG_ALT182 : String = '╢', //Drawing character; right coupler - double vertical/double horizontal. Equal to {Alt+182}.
  16.     DWG_ALT183 : String = '╖', //Drawing character; right upper connector - double vertical/single horizontal. Equal to {Alt+183}.
  17.     DWG_ALT184 : String = '╕'  //Drawing character; right upper connector - single vertical/double horizontal. Equal to {Alt+184}.
  18.   );

Both Type enumerations above are non-functional mockups do demonstrate my needs.

For string enumerations, the techniques proposed so far will often work, though not always, especially if the string is long. It wouldn't make sense to have an enumeration element name that is 50 characters long.

Also, in such cases as above, it is not feasible to use the shown literal values and hash values as element names, either. In most case, it is the element value I need to access and not the element name.  In cases where the required value would always be a number, this would not be an issue. However, the required value often needs to be a string or a character, which enumerated types do not allow.

I need...

Code: Pascal  [Select][+][-]
  1. ShowMessage(DrawingCharacters.DWG_ALT182); //Displays "╢"

rather than...

Code: Pascal  [Select][+][-]
  1. ShowMessage(DrawingCharacters.DWG_ALT182); //Displays "DWG_ALT182"

So, unless types structs are implemented, similar to...

Code: Pascal  [Select][+][-]
  1. Struct CriticalErrors : int64 //Enumerated constant type named "CriticalErrors' - of type int64...
  2.   AccessViolation = 4;
  3.   AppDomainUnloaded = 3;
  4.   BadImageFormat = 5;
  5.   CantUnloadAppDomain = 6;
  6.   ExecutionEngine = 7;
  7.   InvalidProgram = 9;
  8.   NonCritical = 1;
  9.   OutOfMemory = 2;
  10.   StackOverflow = 8;
  11.   ThreadAbort = 10;
  12. End;
  13.  
  14. Struct CalendarDays : String //Enumerated constant type named "CalendarDays' - of type String...
  15.   Day01 = 'Sunday';
  16.   Day02 = 'Monday';
  17.   Day03 = 'Tuesday';
  18.   Day04 = 'Wednesday';
  19.   Day05 = 'Thursday';
  20.   Day06 = 'Friday';
  21.   Day07 = 'Saturday';
  22. End;

I think it will just be more efficient for me to continue using regular constants in Object Pascal.
« Last Edit: April 08, 2020, 08:37:53 pm by GypsyPrince »

PascalDragon

  • Hero Member
  • *****
  • Posts: 1744
  • Compiler Developer
Re: [Solved] Enumerated constant types using existing ordinal types...
« Reply #23 on: April 09, 2020, 01:47:53 pm »
rvk
Quote
Hero Member
*****

Posts: 3950

Re: Getting a string representation of an enum value
« Reply #4 on: August 25, 2016, 04:20:17 pm »
Quote from: fatmonk on August 25, 2016, 04:19:36 pm
I did look at WriteStr() but missed that the result gets sent to the first argument...
I also edited my post to include the GetEnumName-example which is what WriteStr() uses internally.

The line:

Quote
I also edited my post to include the GetEnumName-example which is what WriteStr() uses internally.

from that post in the link I provided earlier is the only reason I went with the GetEnumName() function over the WriteStr() procedure.

I wish there was a way to verify whether that particular statement is true.

As I wrote in my reply above WriteStr is more efficient, because it's handled by the compiler (the compiler directly passes the enum's Ordinal-to-String table as well as the ordinal value of the enum to fpc_write_text_enum while GetEnumName needs to retrieve the pointer to that table using RTTI first).

Code: Pascal  [Select][+][-]
  1. Type ControlCharacters = (
  2.     CTL_AKNWLDGMNT : Char = #6, //Control character; acknowledgment.
  3.     CTL_BACKSPACE : Char = #8,  //Control character; backspace.
  4.     CTL_BELL : Char = #7,       //Control character; bell ("ding") sound.
  5.     CTL_CANCEL : Char = #24;    //Control character; cancel.
  6.     CTL_CRGRTRN : Char = #13,   //Control character; carriage return.
  7.     CTL_DATALINK : Char = #16   //Control character; data link.
  8.   );
  9.  
  10.  
  11.   Type DrawingCharacters = (
  12.     DWG_ALT179 : String = '│', //Drawing character; line - single vertical. Equal to {Alt+179}.
  13.     DWG_ALT180 : String = '┤', //Drawing character; right coupler - single vertical/single horizontal. Equal to {Alt+180}.
  14.     DWG_ALT181 : String = '╡', //Drawing character; right coupler - single vertical/double horizontal. Equal to {Alt+181}.
  15.     DWG_ALT182 : String = '╢', //Drawing character; right coupler - double vertical/double horizontal. Equal to {Alt+182}.
  16.     DWG_ALT183 : String = '╖', //Drawing character; right upper connector - double vertical/single horizontal. Equal to {Alt+183}.
  17.     DWG_ALT184 : String = '╕'  //Drawing character; right upper connector - single vertical/double horizontal. Equal to {Alt+184}.
  18.   );

Both Type enumerations above are non-functional mockups do demonstrate my needs.

For string enumerations, the techniques proposed so far will often work, though not always, especially if the string is long. It wouldn't make sense to have an enumeration element name that is 50 characters long.

Also, in such cases as above, it is not feasible to use the shown literal values and hash values as element names, either. In most case, it is the element value I need to access and not the element name.  In cases where the required value would always be a number, this would not be an issue. However, the required value often needs to be a string or a character, which enumerated types do not allow.

I need...

Code: Pascal  [Select][+][-]
  1. ShowMessage(DrawingCharacters.DWG_ALT182); //Displays "╢"

rather than...

Code: Pascal  [Select][+][-]
  1. ShowMessage(DrawingCharacters.DWG_ALT182); //Displays "DWG_ALT182"

Why not use something like this?

Code: Pascal  [Select][+][-]
  1. {$ScopedEnums On}
  2. type
  3.   TDrawingCharacters = (
  4.     DWG_ALT179,
  5.     DWG_ALT180,
  6.     DWG_ALT181,
  7.     DWG_ALT182,
  8.     DWG_ALT183,
  9.     DWG_ALT184,
  10.   );
  11.  
  12. const
  13.   DrawingCharacters: array[TDrawingCharacters] of Char = (
  14.     '│'
  15.     '┤'
  16.     '╡'
  17.     '╢'
  18.     '╖'
  19.     '╕'
  20.   );
  21.  
  22. // or instead of an operator overload a type helper as mentioned in my previous post
  23. operator := (aValue: TDrawingCharacter): String; // assignment operator for enum -> char is currently not allowed
  24. begin
  25.   Result := DrawingCharacters[aValue];
  26. end;

GypsyPrince

  • Guest
Re: [Solved] Enumerated constant types using existing ordinal types...
« Reply #24 on: April 10, 2020, 01:00:00 am »
Quote
Why not use something like this?

That is actually a great suggestion for most sane people. But in my disturbed mind, it creates too much work to have to maintain down the road. If I ever have to make a change, that method creates 2 separate points to maintain(type and const) whereas structs or regular constants create only 1 point of maintenance. LOL

PascalDragon

  • Hero Member
  • *****
  • Posts: 1744
  • Compiler Developer
Re: [Solved] Enumerated constant types using existing ordinal types...
« Reply #25 on: April 10, 2020, 10:47:10 am »
At least here the compiler will remind you that you forgot to edit the other one, cause if you add an entry to TDrawingCharacters then the compiler will complain about a missing entry in DrawingCharacters.

 

TinyPortal © 2005-2018