Sign in Join

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

Mailing lists / lazarus / 2014-12

Typecast set to byte in mode OBJFPC

9 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Jürgen Hestermann

I have a unit that is compiling in DELPHI mode
but now wanted to change it to OBJFPC mode
(in DELPHI mode I cannot use binary constants %10011100).

But OBJFPC mode I get an "illegal type conversion" with the following code:

---------------------------------------------------------------
type FlagType        = (Flag1,Flag2);
          FlagSetType   = set of FlagType;
var i : SizeInt;
       S : FlagSetTyp;
S := [Flag1];
i := byte(S); // <---- Error "illegal type conversion"
---------------------------------------------------------------

Any idea how to convince the compiler to typecast
the set to byte in OBJFPC mode too?

Reply by email

Joost van der Sluis
to Jürgen Hestermann
J?rgen Hestermann wrote 7 lines
type FlagType = (Flag1,Flag2); FlagSetType = set of FlagType; var i : SizeInt; S : FlagSetTyp; S := [Flag1]; i := byte(S); // <---- Error "illegal type conversion"
i := ord(S); ?

But it's still invalid, though. A set can contain more then 32 items, and then your conversion does not hold anymore.

Joost.

Reply by email

Jürgen Hestermann
to Jürgen Hestermann: I have a unit that is compiling in DELPHI mode but now wanted to change it to OBJFPC mode…
J?rgen Hestermann wrote 9 lines
--------------------------------------------------------------- type FlagType = (Flag1,Flag2); FlagSetType = set of FlagType; var i : SizeInt; S : FlagSetTyp; S := [Flag1]; i := byte(S); // <---- Error "illegal type conversion" ---------------------------------------------------------------

I have already found a solution:

i := SizeInt(S);

works (although I don't know why this is accepted but byte(S) is not).

Reply by email

Jürgen Hestermann
to Joost van der Sluis: i := ord(S); ? But it's still invalid, though. A set can contain more then 32 items, and…
Joost van der Sluis wrote 13 lines
On 12/31/2014 04:40 PM, J?rgen Hestermann wrote: > type FlagType = (Flag1,Flag2); > FlagSetType = set of FlagType; > var i : SizeInt; > S : FlagSetTyp; > > S := [Flag1]; > i := byte(S); // <---- Error "illegal type conversion" i := ord(S); ? But it's still invalid, though. A set can contain more then 32 items, and then your conversion does not hold anymore.

Yes, in DELPHI mode I get an error at compile time when the set is declared larger than a byte which is ok because I can then change all byte(S) calls.

But as already written in another mail I found that i := SizeInt(S) works (is accepted by the compiler). But I am not sure, whether the result is the same. I use the typecasted integer values as an index into an array which is declared like this:

FileStatistikArrayTyp = array[0..(1 shl (ord(High(FlagSetType))+1))-1] of ...; // 0..2^n-1

I don't know whether the typecasted set still starts with zero.

Reply by email

Jürgen Hestermann
to Joost van der Sluis: i := ord(S); ? But it's still invalid, though. A set can contain more then 32 items, and…
Joost van der Sluis wrote 12 lines
On 12/31/2014 04:40 PM, J?rgen Hestermann wrote: > type FlagType = (Flag1,Flag2); > FlagSetType = set of FlagType; > var i : SizeInt; > S : FlagSetTyp; > > S := [Flag1]; > i := byte(S); // <---- Error "illegal type conversion" i := ord(S); ? But it's still invalid, though. A set can contain more then 32 items, and then your conversion does not hold anymore.

It seems that now FlagType is always 4 bytes in size while it was 1 byte in DELPHI mode. That's a real problem because I have stored (and reread) these sets from a binary file which now fails because of the larger set size.
Is this realy compiler mode dependent?
I can't find any documentation about this.

Reply by email

Jürgen Hestermann
to Jürgen Hestermann
J?rgen Hestermann wrote 20 lines
Am 2014-12-31 um 16:55 schrieb Joost van der Sluis: > On 12/31/2014 04:40 PM, J?rgen Hestermann wrote: >> type FlagType = (Flag1,Flag2); >> FlagSetType = set of FlagType; >> var i : SizeInt; >> S : FlagSetTyp; >> >> S := [Flag1]; >> i := byte(S); // <---- Error "illegal type conversion" > > i := ord(S); ? > > But it's still invalid, though. A set can contain more then 32 items, and then your conversion does not hold anymore. It seems that now FlagType is always 4 bytes in size while it was 1 byte in DELPHI mode. That's a real problem because I have stored (and reread) these sets from a binary file which now fails because of the larger set size. Is this realy compiler mode dependent? I can't find any documentation about this.

Sorry, I meant that FlagSETType seems to be 4 bytes now.

Reply by email

Jürgen Hestermann
to Luca Olivetti
Luca Olivetti wrote 8 lines
El 31/12/14 a les 17:33, J?rgen Hestermann ha escrit: >> Is this realy compiler mode dependent? >> I can't find any documentation about this. > Sorry, I meant that FlagSETType seems to be 4 bytes now. http://www.freepascal.org/docs-html/prog/progsu59.html#x66-650001.2.59

Is this valid for set types too?
I think this is what I was looking for:
http://www.freepascal.org/docs-html/prog/progsu61.html

BTW:
The first link I find on Google with "free pascal sets" is http://www.freepascal.org/docs-html/ref/refsu20.html which does not mention any compiler directive dependencies nor any link to $PackSet.
Maybe it should be added.

Reply by email

Jürgen Hestermann
to Jürgen Hestermann
J?rgen Hestermann wrote 13 lines
Am 2014-12-31 um 17:46 schrieb Luca Olivetti: > El 31/12/14 a les 17:33, J?rgen Hestermann ha escrit: > >>> Is this realy compiler mode dependent? >>> I can't find any documentation about this. >> Sorry, I meant that FlagSETType seems to be 4 bytes now. > http://www.freepascal.org/docs-html/prog/progsu59.html#x66-650001.2.59 > > Is this valid for set types too? I think this is what I was looking for: http://www.freepascal.org/docs-html/prog/progsu61.html

It seems that I had to use both directives:

{$PackSet 1}
{$PackEnum 1}

because in OBJFPC mode enum *and* set sizes
are both different to DELPHI mode.
Any other sizes that may have changed?
Is there a list of differences for all modes?

Reply by email