Discussion:
[fpc-pascal] No type info available for this type (Enumerated type with constant assignment)
Dennis
2018-07-23 10:23:59 UTC
Permalink
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
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listi
Michael Van Canneyt
2018-07-23 10:41:36 UTC
Permalink
Post by 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);
No, only normal enumerated types can be used like that.

Michael.
Sven Barth via fpc-pascal
2018-07-23 15:05:02 UTC
Permalink
Post by Dennis
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.

Regards,
Sven
Martok
2018-07-23 15:15:16 UTC
Permalink
Post by Dennis
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

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/
Sven Barth via fpc-pascal
2018-07-23 17:47:55 UTC
Permalink
Post by Dennis
Post by Dennis
Is this kind of enumerated type with constant assignment not
supported
Post by Dennis
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
Martok
2018-07-23 20:34:37 UTC
Permalink
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

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lis
Dennis Poon
2018-07-24 09:06:17 UTC
Permalink
Post by Dennis
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;
Post by Dennis
Regards,
Sven
_______________________________________________
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/

Loading...