<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<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>
</body>
</html>