On 05/07/2019 13:29, Sven Barth via fpc-devel wrote:
J. Gareth Moreton <gar...@moreton-family.com <mailto:gar...@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
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to