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

Michael Van Canneyt michael at freepascal.org
Fri Jul 5 02:17:05 CEST 2019



On Fri, 5 Jul 2019, J. Gareth Moreton wrote:

> It's not so much convenience... it's more the fact that the compiler can 
> and will optimise out "if (MyValue>=ord(Low(TMyType))) and 
> (MyValue<=Ord(High(TMyType))) then" because it has every reason to 
> assume that MyValue cannot possibly take on a value that's less than 
> Low(TMyType) or greater than High(TMyType), hence it gets shortcut and 
> assumed to always be True.

No. MyValue is an integer. Hence the ord().

Obviously the check must be done before the typecast, not after...

Let me make that more explicit:

if MyIntValue is TMyEnum then
   MyValue:=TMyEnum(MyIntValue);
or
MyValye:=MyIntValue as TMyEnum;

That's why I said it it is not at odds with the fact that the compiler can
assume MyValue is always valid.

Michael.


More information about the fpc-devel mailing list