Enumerated Types with Explicitly Assigned Ordinality

By default, the ordinalities of enumerated values start from 0 and follow the sequence in which their identifiers are listed in the type declaration. You can override this by explicitly assigning ordinalities to some or all of the values in the declaration. To assign an ordinality to a value, follow its identifier with = constantExpression, where constantExpression is a constant expression that evaluates to an integer. For example, type size = (small = 5, Medium = 10, Large = small + Medium);

defines a type called size whose possible values include small, Medium, and Large, where Ord (small) returns 5, Ord(Medium) returns 10, and Ord(Large) returns 15.

An enumerated type is, in effect, a subrange whose lowest and highest values correspond to the lowest and highest ordinalities of the constants in the declaration. In the previous example, the size type has 11 possible values whose ordinalities range from 5 to 15. (Hence the type array[size] of Char represents an array of 11 characters.) Only three of these values have names, but the others are accessible through typecasts and through routines such as Pred, succ, Inc, and Dec. In the following example, "anonymous" values in the range of size are assigned to the variable X.

X := Small; // Ord(X) = 5 Y := Size(6); // Ord(X) = 6 Inc(X); // Ord(X) = 7

Any value that isn't explicitly assigned an ordinality has ordinality one greater than that of the previous value in the list. If the first value isn't assigned an ordinality, its ordinality is 0. Hence, given the declaration type SomeEnum = (el, e2, e3 = 1);

SomeEnum has only two possible values: Ord(el) 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.

Enumerated constants without a specific value have RTTI:

whereas enumerated constants with a specific value, such as the following, do not have RTTI:

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment