<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p>Hi everyone,</p>
<p>So there have been a lot of issues going back and forth regarding
case blocks when dealing with enumerations and what happens if the
enum falls outside the valid range of values for some reason,
because the internal domain check is sometimes omitted for
performance reasons, since the value <i>shouldn't</i> be
invalid. Thus, if it is invalid and a jump table is used, an
access violation is raised.</p>
<p>Ultimately, the range check will likely not be included to
safeguard against this, but there are still situations where an
enumeration will take on an out-of-range value, like if it reads
it from an external data file that is corrupted or of an
incompatible version for the program. So I would like to suggest
a new intrinsic called InRange() or IsDomainValid() or something
to that effect that takes an ordinal-type parameter and returns
True if it contains a valid value for the enumeration (always True
if it's a Byte or Word etc) and False if it is outside the valid
domain, hence fulfilling the range check if it's something you
really need.</p>
<p>The reason why I ask for an intrinsic instead of something like
"if (Value >= Low(TEnumType)) and (Value <=
High(TEnumType))" is because such code may be optimised out due to
it logically being impossible for Value to be less than
Low(TEnumType), for example, so it's harder than you think to
check if the variable is in range. Additionally, by having a
generic intrinsic, it can generate more optimised code (e.g. the
equivalent of "if (Value - Low(TEnumType)) < (High(TEnumType) -
Low(TEnumType))", taking advantage of unsigned overflow if Value
is less than the lower bound) and also to check for invalid values
that fall within the maximum range (e.g. if you have something
like "type TEnumType = (etZero, etOne, etSpecial = $FF);" and your
Value holds the equivalent of $FE).</p>
<p>What are your thoughts? Would this be a decent compromise to fix
the case range-check issue that keeps cropping up occasionally? I
know it's causing some people to choose not migrate away from
Delphi, and that is never really a good thing. It should be easy
enough to document as well, putting in an explicit warning for
case to say that if the enumeration is out of range, it may cause
a crash, and to prevent it, use the intrinsic to check beforehand.<br>
</p>
<p>Gareth aka. Kit<br>
</p>
<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>