views:

324

answers:

3

While the following subrange enumeration declaration works:

type
   TReceiptCode = 'A'..'F';

This does not:

type
   TReceiptCode = ' ','A'..'F', 'R';

Nor does

type
    TReceiptCode = ' ','A','B','C','D','E','F','R';

How can i declare a subrange type with non-contiguous values?

+3  A: 

Unfortunately, I don't think there's any way to do that. You can declare a (new) non-contiguous enumeration, or a subrange of an existing type, but not both.

Mason Wheeler
Nobody has come forward in the last 4 months with a solution, so i guess there really is none. Accepted.
Ian Boyd
+3  A: 

Could you use a set instead?

TSomeCharSet= Set of Char;

SomeChars: TSomeCharSet = [' ','A','B','C','D','E','F','R'];

Could be granny and egg situation but I'm not sure what you are using then for :) ...

Well all you are left with then is creating TNonContigousCharRange yourself using a Set or array as the limiting "Range" and raising an exception when it is out of range or having a SetReceiptCode procedure to do a similar thing.

Despatcher
A type is currently defined as a Char. i'd like to limit the values of the characters can can be assigned to that type.
Ian Boyd
Oh, and no, a set wouldn't work because it then takes a set of values, rather than a single value.
Ian Boyd
A set does work, it simply doesn't give you automatic value checking in the compiler, you have to test for membership in that set yourself, but it does give you precisely the data structure you need to achieve that very simply.
Deltics
+2  A: 

To all previous answers I would add simply that the clue is in the name of the type: sub**range**

Simply put, a range has a lower and an upper bound. What you describe is a set (or a subset), not a subrange so of course you cannot express it as a subrange.

Deltics
You're right, of course. I think the applicable term is "oxymoron", not a pejorative but simply descriptive of a phrase that contains contradictory terms.
Argalatyr
Don't forget i'm "making up" a term, since i don't know Delphi's "proper" term for what i'm trying to do. Turns out that Delphi cannot do what i want, so there is no "official" term - so my made-up term is valid. Someone's free to invent another name what i'm trying to achieve.
Ian Boyd
You're missing the point: Q: "Why doesn't this subrange decalration work?" A: "Because it's not a subrange. It's something else". And yes, you're right, the "something else" that you need doesn't exist in Delphi. But that doesn't make your use of the term "sub-range" to describe it valid, not least because "sub-range" *does* have meaning in Delphi a meaning that does not fit what you are trying to achieve (which *is* a "set").
Deltics