[fpc-devel] [Suggestion] Enumeration range-check intrinsic

Ondrej Pokorny lazarus at kluug.net
Tue Jul 2 22:31:04 CEST 2019


On 02.07.2019 21:48, J. Gareth Moreton wrote:
> I should probably test this myself, but if you, say, read a value from 
> a file directly into a variable of type TEnumType, will your patch 
> return False for "if (Value is TEnumType) then" if Value, despite 
> being TEnumType, contains an invalid value?  No problem if it isn't, 
> just that you would have to read an Integer-type first then typecast it.

Yes:

var
   Value: TEnumType;
begin
   Value := TEnumType(-1);
   IsValid := Value is TEnumType; // IsValid gains false

The compiler may not do any optimizations here (like return always true 
if the left side value is the enum type at the right side). This should 
be clearly stated and documented if the feature is added to FPC.

This is similar to the object-is operator that gets evaluated as well 
even if the type of the left-side value is the type at right side:

var
   Value: TPersistent;
begin
   Value := TPersistent(TObject.Create);
   IsValid := Value is TPersistent; // IsValid gains false

Ondrej



More information about the fpc-devel mailing list