[fpc-pascal] array [boolean] and typecasting fail

Martin lazarus at mfriebe.de
Sat Oct 24 01:19:50 CEST 2009


David Emerson wrote:
> I did cast it to boolean! That's why it smells like a bug
>>> var
>>>   b : boolean;
>>>
>>> begin
>>>   b := boolean (6 and 4);
>>>   writeln (t_or_f [b]);
>>>       
A type cast does not changes the value. A typecast merely allows you to 
store a value into a field of a different type

same for enums (boolean is an enum)

type t = (zero, one, two);
var a: t;

a := t(1); // ok
a := t(5); // compiles, gives rangecheck if enabled, otherwise works, 
but a is not any of the enum-values

boolvar := boolean(4); // just puts 4 into the variable. doesn't make 4 
a valid boolean value


So you need to do

  b := (6 and 4) <> 0; // that is a boolean value
  writeln (t_or_f [b]);


Martin



More information about the fpc-pascal mailing list