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

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

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

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.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Peter
On 19/06/15 08:46, Torsten Bonde Christiansen wrote:

> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Michael Van Canneyt
In reply to this post by Torsten Bonde Christiansen


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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Jonas Maebe-2
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


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Michael Van Canneyt


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.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Jonas Maebe-2
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*?


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Adriaan van Os-2
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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Michael Van Canneyt
In reply to this post by Jonas Maebe-2


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  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Bernd Oppolzer
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



Am 20.06.2015 um 14:03 schrieb Michael Van Canneyt:

>
>
> 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  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal