We can currently create enumerated types as follows:
//Boole-Logic - represents an enhanced boolean (True, False, or Neither) value or state...
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:
Struct CriticalErrors : int64 //Enumerated constant type named "CriticalErrors' - of type int64...
AccessViolation = 4;
AppDomainUnloaded = 3;
BadImageFormat = 5;
CantUnloadAppDomain = 6;
ExecutionEngine = 7;
InvalidProgram = 9;
NonCritical = 1;
OutOfMemory = 2;
StackOverflow = 8;
ThreadAbort = 10;
End;
Struct CalendarDays : String //Enumerated constant type named "CalendarDays' - of type String...
Day01 = 'Sunday';
Day02 = 'Monday';
Day03 = 'Tuesday';
Day04 = 'Wednesday';
Day05 = 'Thursday';
Day06 = 'Friday';
Day07 = 'Saturday';
End;
Procedure PrintDay();
Var
dayCurrent : CalendarDays;
Begin
dayCurrent := CalendarDays.Day04;
ShowMessage('Today is ' + dayCurrent + '.');
//Output = "Today is Wednesday."
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:
Procedure PrintDay();
Var
dayCurrent : CalendarDays;
strCurrent : String;
Begin
dayCurrent := CalendarDays.Day04;
strCurrent := dayCurrent; //Because the 'CalendarDays' enumerated type is String, the variable
//strCurrent can be assigned its value.
//Could also be condensed to: strCurrent := CalendarDays.Day04;
ShowMessage('Today is ' + strCurrent + '.');
//Output = "Today is Wednesday."
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.