On 13/07/2019 13:47, J. Gareth Moreton wrote:
Here lies the problem - if you have a enumeration stored in a byte field, say, in a file, and there's a chance it's been corrupted, there's no reliable way to detect that, since even typecasting may not be reliable, and once again I'm implying that enums are unsafe to use, but right now, what else can one do?
Well yes. Reading from a file is like a typecast.

TEnum(SomeInt) implies that you have done the validity check *before* this. Because by using the type you declare that the result is valid.

Of course reading from a file/api you can not do the test before....

But if you want the test afterwards, you must change the entire definition of the type. Changing it for just one statement (the "is") can not work for technical reasons (see my previous post). You do not know what other side-effects you may have done in the statement that typecasted an unchecked/invalid value. If you do not know the list of possible side effects, then you can not test for all of them.

I would guess this would work

{$mode delphi}
{$z1}
type
 tmyFileEnum = (ea, eb, ec, emax = 255);
 tmyenum = (ea, eb, ec);

read(file, AFileEnum);
if AFileEnum is TMyEnum
then AMyEnum := TMyEnum(AFileEnum)
else DoError;
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to