<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">On 02.07.2019 23:34, Jonas Maebe wrote:<br>
</div>
<blockquote type="cite"
cite="mid:976f1032-dc51-036b-f0ce-8cb48c454274@freepascal.org">
<pre class="moz-quote-pre" wrap="">On 02/07/2019 22:31, Ondrej Pokorny wrote:
</pre>
<blockquote type="cite">
<pre class="moz-quote-pre" wrap="">This is similar to the object-is operator that gets evaluated as well
even if the type of the left-side value is the type at right side:
var
Value: TPersistent;
begin
Value := TPersistent(TObject.Create);
IsValid := Value is TPersistent; // IsValid gains false
</pre>
</blockquote>
<pre class="moz-quote-pre" wrap="">
This is an invalid program. If you compile with -CR, the program will
abort with an error when the typecast is performed, because it will get
replaced by an "as" operation. In that sense, "integer as enum" would
indeed be somewhat similar, and -CR might even be extended to perform
the same replacement of explicit typecasts with "as" operators for these
types.</pre>
</blockquote>
<p>If you want to extend -CR to perform "integer as enum" on
enum(integer), please do it consequently and perform "integer as
ShortInt" also on ShortInt(integer) - do range checking on
explicit typecasts for all simple types, not only enums. People
will love you for this:</p>
<p><tt>var</tt><tt><br>
</tt><tt> S: ShortInt;</tt><tt><br>
</tt><tt> I: Integer;</tt><tt><br>
</tt><tt>begin</tt><tt><br>
</tt><tt> I := 130;</tt><tt><br>
</tt><tt> S := ShortInt(I);</tt><tt><br>
</tt><tt>end;</tt><br>
</p>
<p>shall now raise a range check error with -CR. You can even
optimize the compiler to verify constants on compile-time:<br>
</p>
<p><tt>var</tt><tt><br>
</tt><tt> S: ShortInt;</tt><tt><br>
</tt><tt>begin</tt><tt><br>
</tt><tt> S := ShortInt($FF);</tt><tt><br>
</tt><tt>end;</tt><br>
<br>
should not compile any more with -CR. Good idea.</p>
<p>BTW I was always convinced that an explicit typecast switches off
range checking. And now I open the documentation
<a class="moz-txt-link-freetext" href="https://www.freepascal.org/docs-html/prog/progsu65.html">https://www.freepascal.org/docs-html/prog/progsu65.html</a> and I read
this: (citation)<br>
<i>If, at run-time, an index or enumeration type is specified that
is out of the declared range of the compiler, then a run-time
error is generated, and the program exits with exit code 201.
This can happen when doing a typecast (implicit or explicit) on
an enumeration type or subrange type.</i></p>
<p>This means that this program is documented to exit with code 201:</p>
<p><tt>program Project1;</tt><tt><br>
</tt><tt>uses Classes;</tt><tt><br>
</tt><tt>{$R+}</tt><tt><br>
</tt><tt>var</tt><tt><br>
</tt><tt> B: Boolean;</tt><tt><br>
</tt><tt>begin</tt><tt><br>
</tt><tt> B := Boolean(3);</tt><tt><br>
</tt><tt> Writeln(B);</tt><tt><br>
</tt><tt>end.</tt><br>
</p>
<p>Why doesn't it?<br>
</p>
<p>Ondrej</p>
</body>
</html>