[fpc-devel] [Suggestion] Enumeration range-check intrinsic
J. Gareth Moreton
gareth at moreton-family.com
Fri Jul 5 22:34:08 CEST 2019
It feels like a very convoluted way to implement a safe means of reading
an enumeration from a file, but given that that reading incorrect data
files is one of the main causes of enums taking on invalid values, I
have to say that this is the best solution should we not be allowed the
"is" operator or an intrinsic. If that is the case, then I propose this
gets added to TStream!
Gareth aka. Kit
On 05/07/2019 21:27, Sven Barth via fpc-devel wrote:
> Am 05.07.2019 um 22:25 schrieb Sven Barth:
>> Am 05.07.2019 um 08:08 schrieb Sven Barth:
>>> Jonas Maebe <jonas at freepascal.org <mailto:jonas at freepascal.org>>
>>> schrieb am Do., 4. Juli 2019, 21:21:
>>>
>>> On 03/07/2019 09:26, Ondrej Pokorny wrote:
>>> > On 02.07.2019 23:34, Jonas Maebe wrote:
>>> >> Invalid data means undefined behaviour, always. "is" is not a
>>> special
>>> >> case that is immune to this.
>>> >
>>> > Don't you really see the need to handle invalid data with a
>>> /defined/
>>> > behavior?
>>>
>>> My point is that is impossible to do so, so trying to do it in a way
>>> that works in some/most cases, is much more dangerous than
>>> categorically
>>> refusing to try to do it, as it creates a false sense of security.
>>>
>>>
>>> Then how would you read data from e.g. a stream into an enum or
>>> subrange if the stream may contain invalid data?
>> I now did a proof of concept for that task myself:
>>
>> === code begin ===
>>
> [snip]
>> if not s.specialize ReadEnum<TMyEnum>(e) then
>> raise EStreamError.Create('Failed to read enum value');
>> Writeln('Read value: ', e);
>> if not s.specialize ReadEnum<TMyEnum>(e) then
>> raise EstreamError.CReate('Failed to read enum value');
> [snip]
>>
>> === code end ===
>
> Oh and once I've integrated Ryan's implicit specialization support the
> code can become this (or at least I hope so ^^'):
>
> === code begin ===
> if not s.ReadEnum(e) then
> raise EStreamError.Create('Failed to read enum value');
> Writeln('Read value: ', e);
> if not s.ReadEnum(e) then
> raise EstreamError.CReate('Failed to read enum value');
>
> === code end ===
>
> Regards,
> Sven
>
> _______________________________________________
> fpc-devel maillist - fpc-devel at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190705/ff6b3256/attachment.html>
More information about the fpc-devel
mailing list