[comp.sys.mac.programmer] Type cast problem in TP 3.0

hpoppe@ncar.ucar.edu (Herb Poppe) (05/03/91)

The TP 3.0 program below illustrates a problem I am having with type 
casting that is driving me nuts. A set of 0..7 has a size of 1. A 
SignedByte has a size of 1. Yet when I type cast the set of 0..7 to a 
SignedByte the compiler complains that:

"Expression can't be cast to the specified type."

How do I interpret the bits in a set of 0..7 as a byte?

-----------------

program SetTest;

 type
  EightBits = (bit0, bit1, bit2, bit3, bit4, bit5, bit6, bit7);
  Port = set of EightBits;

 var
  dataSize: Integer;
  byteSize: Integer;
  portData: Port;
  portByte: SignedByte;

begin

 dataSize := sizeof(portData);      { = 1 }
 byteSize := sizeof(portByte);      { = 1 }
 portData := [bit0, bit7];

 portByte := SignedByte(portData);  {Expression can't be cast to the 
specified type.}

end.

Herb Poppe             hpoppe@ncar.ucar.edu
NCAR                      (303) 497-1296
1850 Table Mesa Dr.
Boulder, CO  80307-3000

siegel@world.std.com (Rich Siegel) (05/03/91)

In article <11271@ncar.ucar.edu> hpoppe@ncar.ucar.edu (Herb Poppe) writes:
>The TP 3.0 program below illustrates a problem I am having with type 
>casting that is driving me nuts. A set of 0..7 has a size of 1. A 
>SignedByte has a size of 1. Yet when I type cast the set of 0..7 to a 
>SignedByte the compiler complains that:
>
>"Expression can't be cast to the specified type."
>
>How do I interpret the bits in a set of 0..7 as a byte?

	In standard Pascal, casts to or from set-types are illegal. However,
a pointer cast can be used to effect the necessary result:

	s : set of 0..7;
	b : SignedByte;

	b := Ptr(@s)^;	{ recall that the "Ptr" type points to a SignedByte. }

R.

-- 
-----------------------------------------------------------------------
Rich Siegel                              Internet: siegel@world.std.com
Software Engineer                        Applelink: SIEGEL
Symantec Languages Group