Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-06

For .. in .. loops with sets and valued enums

9 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Torsten Bonde Christiansen

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?

I am using fpc 2.6.4

Regards,
Torsten.

Peter
to Torsten Bonde Christiansen
Torsten Bonde Christiansen wrote 16 lines
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.

My guess is that you are iterating a set type, that is wide enough to hold all 5 values.
Iterating a set var produces the expected output.

program Project1;
type
  TMyEnum = (
    a = 1,
    b = 3,
    c = 5
  );
  TMySet = set of TMyEnum;
var
  Item: TMyEnum;
  MySet : TMySet = [a,b,c];
begin
  for Item in MySet do
    Writeln('Value = ', Integer(Item));
end.

Still, I can see your point.

Michael Van Canneyt
to Torsten Bonde Christiansen: Consider the following program: program Project1; type TMyEnum = ( c = 5 ); TMySet = set…
Torsten Bonde Christiansen wrote 28 lines
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.

Michael Van Canneyt
to Jonas Maebe
Jonas Maebe wrote 8 lines
On 20/06/15 11:41, Michael Van Canneyt wrote: > But it is a bug that the compiler does not give an error for the set > type. Please report it. Should it work with any set types at all? (I don't mean set variables, but set types) Or even with any type? It seems very much like something that was accidentally introduced, like what was reverted with http://wiki.freepascal.org/User_Changes_2.6.0#.3COrdinal.3E_in_.3CSet_type.3E_is_now_forbidden

It should definitely work with sets, that was explicitly asked (I am sure, because I asked). I use it in my code.

Michael.

Jonas Maebe
to Michael Van Canneyt
Michael Van Canneyt wrote 15 lines
On Sat, 20 Jun 2015, Jonas Maebe wrote: > On 20/06/15 11:41, Michael Van Canneyt wrote: >> But it is a bug that the compiler does not give an error for the set >> type. Please report it. > > Should it work with any set types at all? (I don't mean set variables, > but set types) Or even with any type? It seems very much like something > that was accidentally introduced, like what was reverted with > http://wiki.freepascal.org/User_Changes_2.6.0#.3COrdinal.3E_in_.3CSet_type.3E_is_now_forbidden It should definitely work with sets, that was explicitly asked (I am sure, because I asked). I use it in my code.

Again: set *variables* or set *types*?

Jonas

Adriaan van Os
to Jonas Maebe
Jonas Maebe wrote
Again: set *variables* or set *types*?

Indeed. Think of

	var i: integer;
	for i in integer do ....

which would be amusing.

Regards,

Adriaan van Os

Michael Van Canneyt
to Jonas Maebe: Again: set *variables* or set *types*? Jonas
Jonas Maebe wrote 18 lines
On 20/06/15 12:01, Michael Van Canneyt wrote: > > > On Sat, 20 Jun 2015, Jonas Maebe wrote: > >> On 20/06/15 11:41, Michael Van Canneyt wrote: >>> But it is a bug that the compiler does not give an error for the set >>> type. Please report it. >> >> Should it work with any set types at all? (I don't mean set variables, >> but set types) Or even with any type? It seems very much like something >> that was accidentally introduced, like what was reverted with >> http://wiki.freepascal.org/User_Changes_2.6.0#.3COrdinal.3E_in_.3CSet_type.3E_is_now_forbidden > > It should definitely work with sets, that was explicitly asked (I am > sure, because I asked). I use it in my code. Again: set *variables* or set *types*?

Ah sorry, I misunderstood your remark. Variables.
I don't see the point of allowing it on a set type. You can just as well use the underlying enum.

Michael.

Bernd Oppolzer
to Michael Van Canneyt

Maybe types should be forbidden at this place completely.

Look at this:

1.) with subrg type - works ok !!

program testin;
type subrg = 1..10;
var i: integer;
begin
   for i in  subrg  do begin
     writeln (i)
   end
end.

2.) with subrg type in in-expression - gives syntax error

program testin;
var i: integer;
begin
   for i in  1.. 10 do begin
     writeln (i)
   end
end.

3.) with set constant in in-expression - works ok

program testin;
var i: integer;
begin
   for i in [1.. 10] do begin
     writeln (i)
   end
end.

Only the 3rd example should be supported, IMO.

Kind regards

Bernd

Michael Van Canneyt wrote 34 lines
On Sat, 20 Jun 2015, Jonas Maebe wrote: > On 20/06/15 12:01, Michael Van Canneyt wrote: >> >> >> On Sat, 20 Jun 2015, Jonas Maebe wrote: >> >>> On 20/06/15 11:41, Michael Van Canneyt wrote: >>>> But it is a bug that the compiler does not give an error for the set >>>> type. Please report it. >>> >>> Should it work with any set types at all? (I don't mean set variables, >>> but set types) Or even with any type? It seems very much like >>> something >>> that was accidentally introduced, like what was reverted with >>> http://wiki.freepascal.org/User_Changes_2.6.0#.3COrdinal.3E_in_.3CSet_type.3E_is_now_forbidden >>> >> >> It should definitely work with sets, that was explicitly asked (I am >> sure, because I asked). I use it in my code. > > Again: set *variables* or set *types*? Ah sorry, I misunderstood your remark. Variables. I don't see the point of allowing it on a set type. You can just as well use the underlying enum. Michael. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal