<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p>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!</p>
<p>Gareth aka. Kit<br>
</p>
<div class="moz-cite-prefix">On 05/07/2019 21:27, Sven Barth via
fpc-devel wrote:<br>
</div>
<blockquote type="cite"
cite="mid:07c4c86f-dc3e-7cd5-4b13-67a939592c12@googlemail.com">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<div class="moz-cite-prefix">Am 05.07.2019 um 22:25 schrieb Sven
Barth:<br>
</div>
<blockquote type="cite"
cite="mid:ebf7c9ae-48a2-5156-294a-0c458f6d732c@googlemail.com">
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<div class="moz-cite-prefix">Am 05.07.2019 um 08:08 schrieb Sven
Barth:<br>
</div>
<blockquote type="cite"
cite="mid:CAFMUeB_Nq5Y5CmswK2qDBFH_VdqszZiBkoDMDS+GAydxxtrozg@mail.gmail.com">
<meta http-equiv="content-type" content="text/html;
charset=UTF-8">
<div dir="auto">
<div>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">Jonas Maebe <<a
href="mailto:jonas@freepascal.org"
moz-do-not-send="true">jonas@freepascal.org</a>>
schrieb am Do., 4. Juli 2019, 21:21:<br>
</div>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">On
03/07/2019 09:26, Ondrej Pokorny wrote:<br>
> On 02.07.2019 23:34, Jonas Maebe wrote:<br>
>> Invalid data means undefined behaviour,
always. "is" is not a special<br>
>> case that is immune to this.<br>
> <br>
> Don't you really see the need to handle invalid
data with a /defined/<br>
> behavior?<br>
<br>
My point is that is impossible to do so, so trying to
do it in a way<br>
that works in some/most cases, is much more dangerous
than categorically<br>
refusing to try to do it, as it creates a false sense
of security.<br>
</blockquote>
</div>
</div>
<div dir="auto"><br>
</div>
<div dir="auto">Then how would you read data from e.g. a
stream into an enum or subrange if the stream may contain
invalid data? <br>
</div>
</div>
</blockquote>
I now did a proof of concept for that task myself:<br>
<br>
=== code begin ===<br>
<br>
</blockquote>
[snip]<br>
<blockquote type="cite"
cite="mid:ebf7c9ae-48a2-5156-294a-0c458f6d732c@googlemail.com">
if not s.specialize ReadEnum<TMyEnum>(e) then<br>
raise EStreamError.Create('Failed to read enum value');<br>
Writeln('Read value: ', e);<br>
if not s.specialize ReadEnum<TMyEnum>(e) then<br>
raise EstreamError.CReate('Failed to read enum value');<br>
</blockquote>
[snip]<br>
<blockquote type="cite"
cite="mid:ebf7c9ae-48a2-5156-294a-0c458f6d732c@googlemail.com"><br>
=== code end ===<br>
</blockquote>
<br>
Oh and once I've integrated Ryan's implicit specialization support
the code can become this (or at least I hope so ^^'):<br>
<br>
=== code begin ===<br>
if not s.ReadEnum(e) then<br>
raise EStreamError.Create('Failed to read enum value');<br>
Writeln('Read value: ', e);<br>
if not s.ReadEnum(e) then<br>
raise EstreamError.CReate('Failed to read enum value');<br>
<br>
=== code end ===<br>
<br>
Regards,<br>
Sven<br>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
fpc-devel maillist - <a class="moz-txt-link-abbreviated" href="mailto:fpc-devel@lists.freepascal.org">fpc-devel@lists.freepascal.org</a>
<a class="moz-txt-link-freetext" href="https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel">https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel</a>
</pre>
</blockquote>
<div id="DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2"><br />
<table style="border-top: 1px solid #D3D4DE;">
<tr>
<td style="width: 55px; padding-top: 13px;"><a href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient" target="_blank"><img src="https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif" alt="" width="46" height="29" style="width: 46px; height: 29px;" /></a></td>
<td style="width: 470px; padding-top: 12px; color: #41424e; font-size: 13px; font-family: Arial, Helvetica, sans-serif; line-height: 18px;">Virus-free. <a href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient" target="_blank" style="color: #4453ea;">www.avast.com</a>
</td>
</tr>
</table><a href="#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2" width="1" height="1"> </a></div></body>
</html>