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

Ondrej Pokorny lazarus at kluug.net
Sun Jul 2 19:55:47 CEST 2017


On 02.07.2017 19:29, Martok wrote:
>   - Case statements execute*precisely one*  of their branches: the statements of
> the matching case label, or the else block otherwise

To support your argument, the current Delphi documentation says the same:

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Declarations_and_Statements

/Whichever caseList has a value equal to that of selectorExpression 
determines the statement to be used. If none of the caseLists has the 
same value as selectorExpression, then the statements in the else clause 
(if there is one) are executed./

According to Delphi documentation, invalid values should point to the 
else clause.

Furthermore, it is OK to use invalid values in caseList as well:

program Project1;

{$APPTYPE CONSOLE}

type
   TMyEnum = (one, two);

{$R+}
var
   E: TMyEnum;
begin
   E := TMyEnum(-1);
   case E of
     one, two: Writeln('valid');
     TMyEnum(-1): Writeln('minus one');
   else
     Writeln('invalid');
   end;
end.

The program above writes 'minus one' in Delphi.

Ondrej

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20170702/27791588/attachment.html>


More information about the fpc-devel mailing list