Sign in Join

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

Mailing lists / fpc-pascal / 2018-07

No type info available for this type (Enumerated type with constant assignment)

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

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

Dennis

I am trying out the examples at
http://wiki.freepascal.org/Enumerated_types but
The following simple program will raise compiler error "No type info available for this typ"

I am using Lazarus 1.8.0  FPC 3.0.4   x86_64-Win64-win32/win64

program testenum;
uses typinfo, classes, sysutils;
type
   TMonthType = (January=1, February, May=5,June, July);
var i : int32;
     s : String;
begin
   for i := Ord(Low(TMonthType)) to Ord(High(TMonthType)) do
     begin
        s:= GetEnumName(TypeInfo(TMonthType), Ord(i)); <- compiler error
pointed at the word TypeInfo
        Writeln(i.ToSTring+' -> '+s);
     end;
   readln;
end.
However, if I change to
   TMonthType = (January, February, May,June, July);

The compiler error will disappear.

Is this kind of enumerated type with constant assignment not supported by FPC 3.0.4?

   TMonthType = (January=1, February, May=5,June, July);

Dennis

Reply by email

Michael Van Canneyt
to Dennis
Dennis wrote 36 lines
I am trying out the examples at http://wiki.freepascal.org/Enumerated_types but The following simple program will raise compiler error "No type info available for this typ" I am using Lazarus 1.8.0  FPC 3.0.4   x86_64-Win64-win32/win64 program testenum; uses typinfo, classes, sysutils; type   TMonthType = (January=1, February, May=5,June, July); var i : int32;     s : String; begin   for i := Ord(Low(TMonthType)) to Ord(High(TMonthType)) do     begin        s:= GetEnumName(TypeInfo(TMonthType), Ord(i)); <- compiler error pointed at the word TypeInfo        Writeln(i.ToSTring+' -> '+s);     end;   readln; end. However, if I change to   TMonthType = (January, February, May,June, July); The compiler error will disappear. Is this kind of enumerated type with constant assignment not supported by FPC 3.0.4?   TMonthType = (January=1, February, May=5,June, July);

No, only normal enumerated types can be used like that.

Michael.

Reply by email

Martok
to Sven Barth
Sven Barth wrote 8 lines
Is this kind of enumerated type with constant assignment not supported by FPC 3.0.4?    TMonthType = (January=1, February, May=5,June, July); The enumeration type itself is supported, however TypeInfo() and thus GetEnumName() and friends are not.

Instead of GetEnumName, you may use ReadStr and WriteStr - the IO functions use their separate tables, which are generated correctly even for enums with assignments.

Regards, Martok

Reply by email

Sven Barth
to Martok

Martok schrieb am Mo., 23. Juli 2018, 17:15:

Show quoted text 15 lines
Am 23.07.2018 um 17:05 schrieb Sven Barth via fpc-pascal: > Is this kind of enumerated type with constant assignment not supported > by FPC 3.0.4? > > TMonthType = (January=1, February, May=5,June, July); > > > The enumeration type itself is supported, however TypeInfo() and thus > GetEnumName() and friends are not. Instead of GetEnumName, you may use ReadStr and WriteStr - the IO functions use their separate tables, which are generated correctly even for enums with assignments.

Though you'll get an access violation if you use a value that's not part of the enum (in the example this will happen when the loop reaches the value 3).

Regards,
Sven

Reply by email

Martok
to Sven Barth
Sven Barth wrote
Though you'll get an access violation if you use a value that's not part of the enum (in the example this will happen when the loop reaches the value 3).

Not an AV - an IOError, so it's easy and safe to catch.

I use stuff like this, with aValue being of a generic type:
  {$IOChecks OFF}
  WriteStr(Result, aValue);
  if IOResult = 107 then
    Result:= '';
  ReadStr(aStr, a);
  Result:= IOResult  106;
  if Result then
    aValue:= a;

Turns out this is currently also the *only* typesafe way to check if an arbitrary ordinal is a valid member of an enum type. But that's a different story ;-)

Regards, Martok

Reply by email

Dennis Poon
to Sven Barth: Dennis schrieb am Mo., 23. Juli 2018, 12:24: The enumeration type itself is supported,…
Sven Barth wrote 13 lines
Dennis < > schrieb am Mo., 23. Juli 2018, 12:24: Is this kind of enumerated type with constant assignment not supported by FPC 3.0.4?    TMonthType = (January=1, February, May=5,June, July); The enumeration type itself is supported, however TypeInfo() and thus GetEnumName() and friends are not.

No only that, the following also gives error
  for a in TMonthType do
     begin
       //a won't be assigned the proper values     end;

Show quoted text 7 lines
Regards, Sven _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email