Jump to content
Aquablue

Get Ordinal Name of an Enumerated Variable - Is This Possible?

Recommended Posts

Hello all, I'm using quite a lot of enumerated variables for a script in progress, since this kind of variable allows usage of the case statement which is very readable.

 

I noticed that SCAR supports usage of the Ord() function, which returns the ordinal of an enumerated variable. So for example, when I have the following:

 

type TestType = (Left, Up, Right, Down);
var Test : TestType;
begin
 Test := Right;
 WriteLn('The ordinal of Test is '+IntToStr(Ord(Test)));
end.

 

This would print "The ordinal of Test is 2".

 

In Delphi, you also have the GetEnumName() function, which as I understand works the other way around, it would return the name of the ordinal of an enumerated variable. So setting Test := 2 in the above example and calling WriteLn('The name of Test is '+GetEnumName(Test)); should print "The name of Test is Right".

 

Does SCAR support a function similar to GetEnumName()?

Link to comment
Share on other sites

There's no such similar function available, but I can check if I might be able to add one. I'm currently focusing my efforts on the development of SCAR 4, which features a far more powerful engine, but I do intend to release SCAR 3.41 soon as a small maintenance update to 3.40 with some bugfixes and minor additions. Whether or not it is possible will depend on the engine itself, but I'm quite confident that internally it simply represents enumerations as ordinal values. So I will have to see if I can figure out a way to extract runtime type information, which might not be possible. I'll let you know whether or not I'm able to do this.

Link to comment
Share on other sites

Well, I have evaluated the possibility of implementing this, and short of majorly overhauling the engine, there's no way to do it. The type information is not kept, not even in the compiler. Rather than building the enum types, the compiler simply converts the enum values into ordinal constants with a temporary link back to the type in the compiler for range validation. The small amount of type information that the compiler does keep is pretty much entirely removed before reaching runtime. I tried to implement a function to grab the enum names from the compiler during runtime by modifying the compiler to keep additional type info, but the compiler destroys everything but the program itself right after compilation, changing that would also be quite messy.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...