Recent

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

GypsyPrince

  • Guest
We can currently create enumerated types as follows:

Code: Pascal  [Select][+][-]
  1. //Boole-Logic - represents an enhanced boolean (True, False, or Neither) value or state...
  2. Type Boologic = (Nay=-1, Nor=0, Aye=1);

However, we can't assign an existing ordinal type to our enumerated type during is declaration.

I know such a feature would be possible to implement because other languages have done it, but would Free Pascal consider adding enumerated constant types (as structures) based on ordinal types?

Example:

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;
  23.  
  24. Procedure PrintDay();
  25.   Var
  26.     dayCurrent : CalendarDays;
  27.  
  28. Begin
  29.     dayCurrent := CalendarDays.Day04;
  30.  
  31.     ShowMessage('Today is ' + dayCurrent + '.');
  32.  
  33.     //Output = "Today is Wednesday."
  34. End;

Note that in each type (struct) declaration, all enumerated elements inherent the same declared type (int64 or String).

Something similar to this can be done using enumerated arrays or sets. However, they are very inefficient to use.  Additionally, this method allows us to encapsulated our constant values, lending to better OOP. Also, in other languages which have implemented this feature, the enumerated type can be read directly  into a variable, routine parameter/argument, property, etc. of that same type.

Example:
 
Code: Pascal  [Select][+][-]
  1. Procedure PrintDay();
  2.   Var
  3.     dayCurrent : CalendarDays;
  4.     strCurrent : String;
  5.  
  6. Begin
  7.     dayCurrent := CalendarDays.Day04;
  8.  
  9.     strCurrent := dayCurrent; //Because the 'CalendarDays' enumerated type is String, the variable
  10.                               //strCurrent can be assigned its value.
  11.  
  12.     //Could also be condensed to: strCurrent := CalendarDays.Day04;
  13.  
  14.     ShowMessage('Today is ' + strCurrent + '.');
  15.  
  16.     //Output = "Today is Wednesday."
  17. End;

Object Pascal already has a somewhat similar feature to this using variable in the record type. And technically, we could use the record type enumeration to achieve the purpose of this feature request. However, the downside to doing so is that I don't know if the programmer would have the ability to lock the variable elements within the record type against editing. Hence, the reason for wanting the same feature using constants so their values can't be inadvertently changed during runtime.
« Last Edit: April 21, 2020, 06:23:04 pm by GypsyPrince »

winni

  • Hero Member
  • *****
  • Posts: 1450
Re: Enumerated constant types using existing ordinal types...
« Reply #1 on: April 05, 2020, 01:37:44 am »
Hi!

In Pascal it is done shorter and more elegant:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.SpeedButton1Click(Sender: TObject);
  2. type
  3.   CalDays= (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);
  4. var
  5.   myDay : CalDays;
  6.   s: string;
  7. begin
  8.   myDay := Thursday;
  9.   WriteStr(s, myDay);
  10.   Showmessage(s);
  11. end;          
  12.  
  13.  

Winni

mercurhyo

  • Full Member
  • ***
  • Posts: 177
Re: Enumerated constant types using existing ordinal types...
« Reply #2 on: April 05, 2020, 01:48:42 am »

However, we can't assign an existing ordinal type to our enumerated type.


I'm wondering why the Ord function exists since 1987 then  :P

https://www.freepascal.org/docs-html/rtl/system/ord.html
DEO MERCHVRIO - Linux Mint latest, Windows 7 - Laz latest fixes- RAD Studio XE7
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

winni

  • Hero Member
  • *****
  • Posts: 1450
Re: Enumerated constant types using existing ordinal types...
« Reply #3 on: April 05, 2020, 01:51:52 am »
Hi!

Ord / Pred / Succ existed in N. Wirth's first Pascal design < 1970.

Winni

mercurhyo

  • Full Member
  • ***
  • Posts: 177
Re: Enumerated constant types using existing ordinal types...
« Reply #4 on: April 05, 2020, 01:55:36 am »
Hi!

Ord / Pred / Succ existed in N. Wirth's first Pascal design < 1970.

Winni

on big machines Sir, yes! on micro-computers, tp introduced this around the year i said
DEO MERCHVRIO - Linux Mint latest, Windows 7 - Laz latest fixes- RAD Studio XE7
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

mercurhyo

  • Full Member
  • ***
  • Posts: 177
Re: Enumerated constant types using existing ordinal types...
« Reply #5 on: April 05, 2020, 01:57:16 am »
so with Transtyping  on one way and Ord the other way back to get indices, you can do merely what you want on sets of elements
DEO MERCHVRIO - Linux Mint latest, Windows 7 - Laz latest fixes- RAD Studio XE7
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

mercurhyo

  • Full Member
  • ***
  • Posts: 177
Re: Enumerated constant types using existing ordinal types...
« Reply #6 on: April 05, 2020, 01:57:59 am »
Yes. But your function does not do what I'm asking.

Read about transtyping/ typecasts
« Last Edit: April 05, 2020, 02:00:02 am by mercurhyo »
DEO MERCHVRIO - Linux Mint latest, Windows 7 - Laz latest fixes- RAD Studio XE7
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

mercurhyo

  • Full Member
  • ***
  • Posts: 177
Re: Enumerated constant types using existing ordinal types...
« Reply #7 on: April 05, 2020, 02:02:40 am »
your feature request, as you know how to typecast and index back, is useless , the RangeChecking compiler option is clever enough when you test your "awesome" LOL code ;)
« Last Edit: April 05, 2020, 02:05:06 am by mercurhyo »
DEO MERCHVRIO - Linux Mint latest, Windows 7 - Laz latest fixes- RAD Studio XE7
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

GypsyPrince

  • Guest
Re: Enumerated constant types using existing ordinal types...
« Reply #8 on: April 05, 2020, 02:03:45 am »
It is not useless if I have a use for it.

mercurhyo

  • Full Member
  • ***
  • Posts: 177
Re: Enumerated constant types using existing ordinal types...
« Reply #9 on: April 05, 2020, 02:07:15 am »
It is not useless if I have a use for it.
yea we all have such needs !

Here I NEED a feature request that is very IMPORTANT! a compiler that writes my programs on its own, because I don't plan to remove my gloves in front of my keyboard, each time it's cold in my room
 :P :D
DEO MERCHVRIO - Linux Mint latest, Windows 7 - Laz latest fixes- RAD Studio XE7
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

winni

  • Hero Member
  • *****
  • Posts: 1450
Re: Enumerated constant types using existing ordinal types...
« Reply #10 on: April 05, 2020, 02:13:12 am »
Wow... this place is such a waste of time...

So why are you here?

This is a Pascal forum.

If you want to have some odd C constructs to be translated 1:1 - this is not the right place.

It is already all there. But you have to dive into Pascal.

Pascal was there before C was created.
C has learned a lot from Pascal.
But not enough.

Winni

mercurhyo

  • Full Member
  • ***
  • Posts: 177
Re: Enumerated constant types using existing ordinal types...
« Reply #11 on: April 05, 2020, 02:18:57 am »
NOW with your permission, I consider you as friend @Winni
Welcome to my Mean Humour World hehehe
Thank you!

@Winni +1000
 :D
DEO MERCHVRIO - Linux Mint latest, Windows 7 - Laz latest fixes- RAD Studio XE7
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

winni

  • Hero Member
  • *****
  • Posts: 1450
Re: Enumerated constant types using existing ordinal types...
« Reply #12 on: April 05, 2020, 02:43:48 am »

on big machines Sir, yes! on micro-computers, tp introduced this around the year i said

Nope!

UCSD Pascal for 80 different small and not so small computers had it since 1978.
UCSD Pascal war sold from 1979 on as Apple Pascal for the Apple II.

I  have worked with it!

Winni


GypsyPrince

  • Guest
Re: Enumerated constant types using existing ordinal types...
« Reply #13 on: April 08, 2020, 12:38:12 am »
@winni

Quote
In Pascal it is done shorter and more elegant:

Code: Pascal  [Select]
procedure TForm1.SpeedButton1Click(Sender: TObject);
type
  CalDays= (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);
var
  myDay : CalDays;
  s: string;
begin
  myDay := Thursday;
  WriteStr(s, myDay);
  Showmessage(s);
end;

Unfortunately, the declaration

Code: Pascal  [Select][+][-]
  1. type
  2.   CalDays= (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);

equates to

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

Because of this, a simplistic implementation of...

Code: Pascal  [Select][+][-]
  1. ShowMessage(TCalDays.Sunday);

will result in the numeric value being displayed/referenced rather than the string I'm looking for.

Ideally, I should be able to do this to be efficient...

Code: Pascal  [Select][+][-]
  1. type
  2.   TCalDays= (day01='Sunday', day02='Monday', day03='Tuesday', day04='Wednesday', day05='Thursday', day06='Friday', day07='Saturday');
  3.  
  4. ShowMessage(TCalDays.day03); //Displays "Tuesday"
  5.  

However, as is currently implemented, in order to get to the string value (incorrectly positioned as the enumerated value's name) I must add the 2 additional steps of assigning the enumeration element to a string variable, then use the WriteStr() function to extract the element name (rather than properly extracting the element's value).

Code: Pascal  [Select][+][-]
  1. var
  2.   myDay : CalDays;
  3.   s: string;
  4.  
  5. begin
  6.   myDay := Thursday; //2 extra steps to convert the enumeration element's name to a value.
  7.   WriteStr(s, myDay);
  8.  
  9.   Showmessage(s);
  10. end;

So, for me anyway, I see it as more efficient that I continue to use regular string constants rather than an enumerated type when dealing with string values.
« Last Edit: April 08, 2020, 07:48:23 am by GypsyPrince »

winni

  • Hero Member
  • *****
  • Posts: 1450
Re: Enumerated constant types using existing ordinal types...
« Reply #14 on: April 08, 2020, 12:55:26 am »
@winni

Unfortunately, the declaration

Code: Pascal  [Select][+][-]
  1. type
  2.   CalDays= (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);

equates to

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

No

 

TinyPortal © 2005-2018