Sign in Join

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

Mailing lists / fpc-pascal / 2024-06

Ordinal casting to enums

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Hairy Pixels

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

Ondrej Pokorny
to Hairy Pixels

writeln() converts the ordinal value to the value name and if the entry is not found, the 107 error is raised.

Ondrej

Hairy Pixels wrote 22 lines
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 _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Ondrej Pokorny
to Ondrej Pokorny
Ondrej Pokorny wrote 8 lines
writeln() converts the ordinal value to the value name and if the entry is not found, the 107 error is raised. Ondrej On 06.06.2024 06:22, Hairy Pixels via fpc-pascal wrote: > [ ... ] > how this works at runtime is another question.

To find out, compile FPC with debug info and step into the writeln() call with F7 (multiple times until you get into fpc_shortstr_enum_intern()):

#0 FPC_SHORTSTR_ENUM_INTERN(4284424, 4284488, POINTER(nil), POINTER($00326000), , 12) at sstrings.inc:575
#1 fpc_write_text_enum(POINTER($00416008), POINTER($00416048), 0, TEXT (), 12) at text.inc:1272
#2 :16

Ondrej

Hairy Pixels
to Ondrej Pokorny

oh, it's writeln which is doing the runtime check, thanks. Doing math on enums is probably not a smart idea though. I wonder why a subscript like [] was never added to enums. It would be nice to do:

f := Fruit[banana.index + 1];

instead of dealing with raw values. A runtime check for creating enums was ordinals would be smart also, like:

f := Fruit.Create(10);

instead of blindly cast ordinals to enums which may be invalid.

Show quoted text 7 lines
On Jun 6, 2024, at 2:33 PM, Ondrej Pokorny wrote: To find out, compile FPC with debug info and step into the writeln() call with F7 (multiple times until you get into fpc_shortstr_enum_intern()): #0 FPC_SHORTSTR_ENUM_INTERN(4284424, 4284488, POINTER(nil), POINTER($00326000), , 12) at sstrings.inc:575 #1 fpc_write_text_enum(POINTER($00416008), POINTER($00416048), 0, TEXT (), 12) at text.inc:1272 #2 :16

Regards,
Ryan Joseph