[fpc-pascal] Ordinal casting to enums

Hairy Pixels genericptr at gmail.com
Thu Jun 6 06:22:26 CEST 2024


This program below crashes because the value 12 is not in the enum. I was curious though, how does it know this? Does it have to do a linear search through the enum to find the value? I know "succ" fails at compile time because the enum has assignments but how this works at runtime is another question.

type
  Fruit = (apple = 10, 
           banana, 
           pear = 20,
           citrus);
var
  f: Fruit;
  i: Integer;
begin
  i := 12;
  f := Fruit(i);
  writeln(f);
end;

Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list