[fpc-devel] Dangerous optimization in CASE..OF

Ondrej Pokorny lazarus at kluug.net
Sun Apr 15 20:17:45 CEST 2018

On 15.04.2018 19:46, Martok wrote:
> Am 15.04.2018 um 11:09 schrieb Ondrej Pokorny:
>> Please note that I want to support all ordinal values on the left side
>> of the operator. You can then validate a value of variable of the enum
>> type itself.
> Useful indeed! It still strikes me as weird that one will need to add this check
> many times when porting code from Delphi, but at least now one can.
> Jonas will probably argue that this is a tautology and should always evaluate to
> true because a variable of a type by definition is of that type, regardless of
> content, but it might be considered as somewhat in line with how "var is class"
> handles nil values as "not an instance of class".

Yes, unfortunately the "implementation detail"/"undefined behavior" 
mantra can strike us here as well :/ Actually it can strike us 
everywhere. If you take it to an extreme, the Ord(E) result is undefined 
for an invalid enum value as well:

   TMyEnum = (zero, one);
   E: TMyEnum;
   E := TMyEnum(5);
   case Ord(E) of
     Ord(zero): A;
     Ord(two): B;

Who knows what will be executed. A, B or C or nothing? Undefined 
behavior indeed...


More information about the fpc-devel mailing list