[fpc-pascal] For .. in .. loops with sets and valued enums
Michael Van Canneyt
michael at freepascal.org
Sat Jun 20 11:41:35 CEST 2015
On Fri, 19 Jun 2015, Torsten Bonde Christiansen wrote:
> Consider the following program:
>
> program Project1;
>
> type
> TMyEnum = (
> a = 1,
> b = 3,
> c = 5
> );
> TMySet = set of TMyEnum;
> var
> Item: TMyEnum;
>
> begin
> for Item in TMySet do
> Writeln('Value = ', Integer(Item));
> end.
>
>
> I would expect the output of the program to contain the values 1, 3 and 5 only. But the actual output is:
> Value = 1
> Value = 2
> Value = 3
> Value = 4
> Value = 5
>
> Is this a bug or a know limitation of the "for ... In ... do" loop?
This is a known limitation: you cannot use for..in.. with enums that have assigned values.
The compiler gives an error if you do:
home: >cat tef.pp
type
TMyEnum = (a = 1,b = 3,c = 5);
var
Item: TMyEnum;
begin
for Item in TMyEnum do
Writeln('Value = ', Integer(Item));
end.
home: >fpc tef.pp
tef.pp(6,16) Error: For in loop cannot be used for the type "TMyEnum"
tef.pp(8,7) Fatal: There were 1 errors compiling module, stopping
But it is a bug that the compiler does not give an error for the set type. Please report it.
If you use a variable as indicated by Peter, it will work.
Michael.
More information about the fpc-pascal
mailing list