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

Ondrej Pokorny lazarus at kluug.net
Fri Jul 5 12:20:19 CEST 2019


On 05.07.2019 11:08, Martok wrote:
> Am 05.07.2019 um 02:52 schrieb Michael Van Canneyt:
>> With this sentence you forbid storing or communicating enumerated values in any way:
>> file, database, over network. It can be used only in a computer program and never
>> leave the context of the running program under any form. Because as soon as
>> it is somehow communicated, there is a chance it becomes invalid in return
>> communication.
> This is the conclusion from Jonas' arguments, which he explicitly confirmed when
> we had the discussion two+ years ago.
>
> You're also missing another important type of 'communication': calling non-FPC
> APIs or even just FPC libraries of possibly different version (see: Run-Time
> Packages!)
>
> Anything that even looks like a subrange cannot be used for these interfaces.

You may use enumerations for these interfaces - you may just not fill 
them directly but through an integer variable in between when reading 
from outside.

IMO this is not a problem - most modern interfaces work like this 
anyway. E.g. for the database you have the TField.AsInteger:

if MyField.AsInteger is TMyEnum then
   MyEnumValue := TMyEnum(MyField.AsInteger)
else
   // do something in case of invalid value

The above code will work perfectly and safely.

For REST or SOAP APIs etc. you have a JSON or XML reader that again 
returns an Extended (JSON) or string (XML) so again this is no problem 
at all.

Only the old-fashioned direct reads (from files, streams etc.) must be 
done with an extra integer variable in between. But that is safer anyway 
because e.g. if you add new enumeration values the SizeOf(TMyEnum) may 
increase and then you have a big problem anyway.

So all-in-all the 2 features solve the problem:
1.) IS/AS operators. They will save some typing (check for low-high bounds).
2.) Additional compiler built-in enumeration value checks when 
range-checking is on. They will help to find places where an invalid 
enumeration value may occur and so the program code can be fixed during 
debugging.

Ondrej



More information about the fpc-devel mailing list