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

J. Gareth Moreton gareth at moreton-family.com
Fri Jul 5 15:34:03 CEST 2019


On 05/07/2019 13:29, Sven Barth via fpc-devel wrote:
> J. Gareth Moreton <gareth at moreton-family.com 
> <mailto:gareth at moreton-family.com>> schrieb am Fr., 5. Juli 2019, 14:02:
>
>     I'm glad we agree on the "is" operator.  Personally I would like to
>     extend it so it accepts "if (I is TMyEnum) then" when I is also of
>     type
>     TMyEnum, since it may be the case that you're reading I directly
>     from a
>     stream, either individually or as part of a record, in which case
>     converting I from an Integer to TMyEnum is a little inconvenient. 
>     When
>     you get down to the compiled assembly language it shouldn't really
>     matter anyway.
>
>
> It might not matter from the assembly point of view, but it matters 
> from the language point of view. When we say that enum values only 
> contain valid values then "myenum is TMyEnum" *must* always evaluate 
> to true, because otherwise there's an exception of the rule that enums 
> only contain valid values.
>
> Regards,
> Sven
>
In this situation, the exception is kind of needed, and was suggested 
for this purpose in the first place.  If you read an enum from a stream 
or any kind of external source, then there is a chance it will take on 
an invalid value and needs to be trapped. I don't think anyone wants to 
be forced to ditch enumerations for integers and C-style constants so as 
to not fall foul of this quirk.  "is" is to catch an enumeration taking 
on an invalid value, thus allowing customised error handling to be 
executed, so other expressions can be optimised on the assumption that 
the enum is definitely valid by that point.

Gareth aka. Kit



---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190705/28b43686/attachment.html>


More information about the fpc-devel mailing list