Sign in Join

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

Mailing lists / fpc-devel / 2005-02

TShiftState as enum

16 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

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

Mattias Gaertner
TShiftState is defined as TShiftState = set of (...);

How can I iterate through the enums? If not, can we split and add an enum:

TShiftStateEnum = (...)
TShiftState = set of TShiftStateEnum;

?

Mattias

Reply by email

Marco van de Voort
to Mattias Gaertner
Show quoted text 9 lines
TShiftState is defined as TShiftState = set of (...); How can I iterate through the enums? If not, can we split and add an enum: TShiftStateEnum = (...) TShiftState = set of TShiftStateEnum; ?

Of course that is possible. It requires some imagination though (and a feel for obfuscated Pascal)

type myset = set of ( a1,a2,a3,a4,a5,a6);
// workaround types
const
     beginmyset=ord(low(myset));
     endmyset=ord(high(myset));
type
     uglyenum = beginmyset..endmyset;
     uglyset= set of uglyenum;
var b : myset;
    i : integer;
begin
  b:=[a1,a2,a3];
  for i:=ord(low(myset)) to ord(high(myset)) do
    if i in uglyset(b) then writeln(i);
end.

Reply by email

Marco van de Voort
to Marco van de Voort
Show quoted text 12 lines
> TShiftState is defined as TShiftState = set of (...); > > How can I iterate through the enums? If not, can we split and add an > enum: > > TShiftStateEnum = (...) > TShiftState = set of TShiftStateEnum; > > ? Of course that is possible. It requires some imagination though (and a feel for obfuscated Pascal)

The example fails in Delphi 6 btw, but works in FPC :-) Delphi does not allow low/high on sets

(I vote to keep this FPC extension btw)

Reply by email

DrDiettrich
to Marco van de Voort
Marco van de Voort wrote 18 lines
> > TShiftState is defined as TShiftState = set of (...); > > > > How can I iterate through the enums? If not, can we split and add an > > enum: > > > > TShiftStateEnum = (...) > > TShiftState = set of TShiftStateEnum; > > > > ? > > Of course that is possible. It requires some imagination though (and a feel > for obfuscated Pascal) The example fails in Delphi 6 btw, but works in FPC :-) Delphi does not allow low/high on sets (I vote to keep this FPC extension btw)

Can somebody enlighten me, what code exactly fails in D6? What extension does FPC have, that Delphi doesn't have?

And what "iteration" is desired? I'd use: For Low(x) To High(x)...

Let me add some more notes, regarding Delphi compatibility:

Older Delphi (and TP?) versions implemented sets of subranges (e.g. set of 510..515) by "stripping" unused bytes in front of the set. The lowest bit in a set variable always had an ordinal value of 2^n, and above set would occupy 2 bytes, equaling an set of (504..519). In newer Delphi versions the lowest bit exactly corresponds to the lowest ordinal value in the range, the above set only occupies one byte (510..517).

AFAIR D6 introduced "open" enums, with arbitrary (discontinuous...) values for the members. IMO this only is an attempt to closer reflect C enums, but sets of such enumerated types are not allowed.

I do not ask for any compatibility in these cases, in detail when the Delphi implementation differs amongst compiler versions.

DoDi

Reply by email

Mattias Gaertner
to Marco van de Voort: The example fails in Delphi 6 btw, but works in FPC :-) Delphi does not allow low/high on…
Show quoted text 15 lines
> > TShiftState is defined as TShiftState = set of (...); > > > > How can I iterate through the enums? If not, can we split and add > > an enum: > > > > TShiftStateEnum = (...) > > TShiftState = set of TShiftStateEnum; > > > > ? > > Of course that is possible. It requires some imagination though (and > a feel for obfuscated Pascal) The example fails in Delphi 6 btw, but works in FPC :-)

Interesting :)

But in this case: why not simply add one line to classes.pp?

Delphi does not allow low/high on sets (I vote to keep this FPC extension btw)

Yes.

Mattias

Reply by email

Vinzent Hoefler
to Marco van de Voort: Of course that is possible. It requires some imagination though (and a feel for…
Marco van de Voort wrote 18 lines
type myset = set of ( a1,a2,a3,a4,a5,a6); // workaround types const beginmyset=ord(low(myset)); endmyset=ord(high(myset)); type uglyenum = beginmyset..endmyset; uglyset= set of uglyenum; var b : myset; i : integer; begin b:=[a1,a2,a3]; for i:=ord(low(myset)) to ord(high(myset)) do if i in uglyset(b) then writeln(i); end.

"Almost every complex solution ...":

type
   My_Set = set of (a1, a2, a3, a4, a5, a6);
// workaround type
type
   Ugly_Enum = Low (My_Set) .. High (My_Set);
var
   b : My_Set;
   i : Ugly_Enum;
begin
   b := [a1, a2, a3, a5];
   for i := Low (b) to High (b) do
      if i in b then
         WriteLn (Ord (i));
end.

Personally I would prefer to first define the enum and the "set of enum" then. While looking at it: Notice that the code above just reverses the order of the definitions which you need anyway. Quite awkward, I think.

Vinzent.

Reply by email

Marco van de Voort
to Mattias Gaertner: On Thu, 17 Feb 2005 08:58:01 +0100 (CET) Interesting :) But in this case: why not simply…
Show quoted text 5 lines
(Marco van de Voort) wrote: > > The example fails in Delphi 6 btw, but works in FPC :-) Interesting :)

Yeah, D2005 for IN syntax is also useful for this (if it operates on sets)

But in this case: why not simply add one line to classes.pp?

That is not as much fun!

(But I meanwhile already committed and forwarded the tag (so it will be in 1.9.8))

Show quoted text 5 lines
> Delphi does not allow low/high on sets > > (I vote to keep this FPC extension btw) Yes.

I also submitted it as bug/wishlist to Borland.

Reply by email

Jose Manuel
to Mattias Gaertner: On Thu, 17 Feb 2005 08:58:01 +0100 (CET) Interesting :) But in this case: why not simply…
Show quoted text 22 lines
> > > > > TShiftStateEnum = (...) > > > TShiftState = set of TShiftStateEnum; > > > > > > ? > > > > Of course that is possible. It requires some imagination though (and > > a feel for obfuscated Pascal) > > > The example fails in Delphi 6 btw, but works in FPC :-) Interesting :) But in this case: why not simply add one line to classes.pp? > Delphi does not allow low/high on sets > > (I vote to keep this FPC extension btw) Yes.

Well, sets are by definition a kind of bag. No enumeration or any kind of order are suited to sets.
Can be handy, I agree, under certain situations, but then we are getting back to C.
I think sets are sets and enumerated types are enumerated types, and Pascal is Pascal-

Mattias

Only IMHO, of course

JMR

Reply by email

Nikolay Nikolov
to Jose Manuel
Jose Manuel wrote 8 lines
Well, sets are by definition a kind of bag. No enumeration or any kind of order are suited to sets. Can be handy, I agree, under certain situations, but then we are getting back to C. I think sets are sets and enumerated types are enumerated types, and Pascal is Pascal-

Ok, but in pascal the elements in a set can only be of ordered type anyway.

Reply by email

Jose Manuel
to Nikolay Nikolov
Show quoted text 9 lines
Jose Manuel wrote: >I think sets are sets and enumerated types are enumerated types, and Pascal >is Pascal- > > Ok, but in pascal the elements in a set can only be of ordered type anyway.

Yes, of course. Maybe I haven't explained myself. Indeed, and you are quite right you'got to define a set of ... (an scalar or enumerated type you've previously defined), but once you're created the set is a "set", there are no order established. I think this is no place nor time to talk about Cantor or whether a subset is part of itself, etc. I just keep on my opinion (until you refuse me with arguments, and I will accept them gratefully, don't doubt on that), that casting that way is against Pascal's spirit (and against Boolean Algebra, I think).

Anyway, it's only my opinion.
As Michael would say, there are more (and more neccessary) things to do than to speak in this list about angels' sex.

JMR

Reply by email

Jose Manuel
to DrDiettrich: Can somebody enlighten me, what code exactly fails in D6? What extension does FPC have,…
Show quoted text 11 lines
Can somebody enlighten me, what code exactly fails in D6? What extension does FPC have, that Delphi doesn't have? And what "iteration" is desired? I'd use: For Low(x) To High(x)... Let me add some more notes, regarding Delphi compatibility: Older Delphi (and TP?) versions implemented sets of subranges (e.g. set of 510..515)

i don't want to become heavy, but a subrange it's a subrange and a set is a set.
If you'd use Low() and High(), your treating a set as if it were an array, that can be handy sometimes, but strictly speaking, a set has no lower or higher elements.
As far as I can remember, in TP, sets were represented with a single byte, that is, the 255 bits were on or off (1 or 0) and represented the existance or the lack of it in the set.
AFAICK, you can check for a determinate value in a set, but you can't "navigate" through it as if it were an array. OK, you can use Boolean operators (Union, Intersection, Rest...) and the Include, Exclude functions, etc..., if you feel more comfortable. But there's no firt element, there are no repeated elements, etc... It's a set

AFAIR D6 introduced "open" enums, with arbitrary (discontinuous...) values for the members. IMO this only is an attempt to closer reflect C enums, but sets of such enumerated types are not allowed.

That's right. Enumerated open types (C-alike), such as (GREY=14, RED, BLUE=21, ...) were introduced in D6, but anyway don?t have nothing to do with sets.

I do not ask for any compatibility in these cases, in detail when the Delphi implementation differs amongst compiler versions.

I really don't know, but I guess you are right, it must be a compiler dependence.

JMR

Reply by email

Peter Vreman
to DrDiettrich: Can somebody enlighten me, what code exactly fails in D6? What extension does FPC have,…
Show quoted text 6 lines
Older Delphi (and TP?) versions implemented sets of subranges (e.g. set of 510..515) by "stripping" unused bytes in front of the set. The lowest bit in a set variable always had an ordinal value of 2^n, and above set would occupy 2 bytes, equaling an set of (504..519). In newer Delphi versions the lowest bit exactly corresponds to the lowest ordinal value in the range, the above set only occupies one byte (510..517).

Supported by FPC

AFAIR D6 introduced "open" enums, with arbitrary (discontinuous...) values for the members. IMO this only is an attempt to closer reflect C enums, but sets of such enumerated types are not allowed.

Supported by FPC

Reply by email

Marco van de Voort
to Peter Vreman
Show quoted text 8 lines
> Older Delphi (and TP?) versions implemented sets of subranges (e.g. set > of 510..515) by "stripping" unused bytes in front of the set. The lowest > bit in a set variable always had an ordinal value of 2^n, and above set > would occupy 2 bytes, equaling an set of (504..519). In newer Delphi > versions the lowest bit exactly corresponds to the lowest ordinal value > in the range, the above set only occupies one byte (510..517). Supported by FPC
{$mode delphi}
type aenum=510..517;
     aset = set of aenum;
begin
  Writeln(sizeof(aset));
end.
xbla.pp(5,25) Error: illegal type declaration of set elements
xbla.pp(10,4) Fatal: There were 1 errors compiling module, stopping

Redone with 200..207; -> size = 32 byte = 256 element, so the behaviour of the older version.

Reply by email

Alexey Barkovoy
to Marco van de Voort
Show quoted text 13 lines
{$mode delphi} type aenum=510..517; aset = set of aenum; begin Writeln(sizeof(aset)); end. xbla.pp(5,25) Error: illegal type declaration of set elements xbla.pp(10,4) Fatal: There were 1 errors compiling module, stopping

Delphi dowsn't allow sets with ordinal values larger than 255 too:

Borland Delphi Version 13.0 Copyright (c) 1983,99 Inprise Corporation 1.pas(2) Error: Sets may have at most 256 elements 1.pas(8)

=========================================

Borland Delphi for Win32 compiler version 17.0
Copyright (c) 1983,2004 Borland Software Corporation 1.pas(2) Error: E2028 Sets may have at most 256 elements 1.pas(8)

Reply by email

DrDiettrich
to Alexey Barkovoy
Alexey Barkovoy wrote
Delphi dowsn't allow sets with ordinal values larger than 255 too:

That's incorrect.

Borland Delphi Version 13.0 Copyright (c) 1983,99 Inprise Corporation 1.pas(2) Error: Sets may have at most 256 elements

Sets are restricted to a maximum of 256 members, but the ordinal values of the members can be higher.

DoDi

Reply by email

Alexey Barkovoy
to DrDiettrich
Show quoted text 6 lines
> Delphi dowsn't allow sets with ordinal values larger than 255 too: That's incorrect. ..... Sets are restricted to a maximum of 256 members, but the ordinal values of the members can be higher.

As I've already posted: sample code below not compile in any Delphi version. You can try it yourself.

type aenum=510..517;
     aset = set of aenum;
begin
  Writeln(sizeof(aset));
end.

Delphi 2005 emits:
Borland Delphi for Win32 compiler version 17.0
Copyright (c) 1983,2004 Borland Software Corporation 1.pas(2) Error: E2028 Sets may have at most 256 elements 1.pas(7)

Reply by email