On 02.07.2017 18:28, Jonas Maebe wrote:
On 02/07/17 18:26, Ondrej Pokorny wrote:
Allow me a stupid question: how to convert an integer to enum with range checking?

The current possibilities and possibly improvements have been mentioned elsewhere in this thread already
* http://lists.freepascal.org/pipermail/fpc-devel/2017-July/038013.html
* http://lists.freepascal.org/pipermail/fpc-devel/2017-July/038014.html

Thanks, so there is no enumeration range checking from the compiler at all :/ Everything has to be done manually :/

1.) if (I>=Ord(Low(TMyEnum))) and (I<=Ord(High(TMyEnum))) then

It's long and ugly and it is manual checking that the $RANGECHECKS directive has no effect to. (Yes, I use it in my code.)

2.) function ValueInEnumRange(TypeInfo : PTypeInfo; AValue : Integer) : boolean;

This still involves a manual checking.

Another problem: RTTI is not generated for enums with explicit indexes, if I am not mistaken: TEnum = (two = 2, four = 4).

---

IMO FPC/Pascal lacks an assignment operator for enums with range checking. Something like:

EnumValue := IntegerValue as TEnum;

Are there any disadvantages of the enum-AS operator that prevents its introduction?

Ondrej
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to