<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.2017 18:28, Jonas Maebe wrote:<br>
</div>
<blockquote type="cite"
cite="mid:8109d1c1-e399-4f34-c09e-de405009326e@freepascal.org">On
02/07/17 18:26, Ondrej Pokorny wrote:
<br>
<blockquote type="cite">Allow me a stupid question: how to convert
an integer to enum with range checking?
<br>
</blockquote>
<br>
The current possibilities and possibly improvements have been
mentioned elsewhere in this thread already
<br>
*
<a class="moz-txt-link-freetext" href="http://lists.freepascal.org/pipermail/fpc-devel/2017-July/038013.html">http://lists.freepascal.org/pipermail/fpc-devel/2017-July/038013.html</a>
<br>
*
<a class="moz-txt-link-freetext" href="http://lists.freepascal.org/pipermail/fpc-devel/2017-July/038014.html">http://lists.freepascal.org/pipermail/fpc-devel/2017-July/038014.html</a></blockquote>
<br>
Thanks, so there is no enumeration range checking from the compiler
at all :/ Everything has to be done manually :/<br>
<br>
1.) if (I>=Ord(Low(TMyEnum))) and (I<=Ord(High(TMyEnum))) then<br>
<br>
It's long and ugly and it is manual checking that the $RANGECHECKS
directive has no effect to<span style="color: rgb(0, 0, 0);
font-family: monospace; font-size: medium; font-style: normal;
font-variant-ligatures: normal; font-variant-caps: normal;
font-weight: normal; letter-spacing: normal; orphans: 2;
text-align: start; text-indent: 0px; text-transform: none;
white-space: normal; widows: 2; word-spacing: 0px;
-webkit-text-stroke-width: 0px; text-decoration-style: initial;
text-decoration-color: initial; display: inline !important; float:
none;"></span>. (Yes, I use it in my code.)<br>
<br>
2.) function ValueInEnumRange(TypeInfo : PTypeInfo; AValue :
Integer) : boolean;<br>
<br>
This still involves a manual checking.<br>
<br>
Another problem: RTTI is not generated for enums with explicit
indexes, if I am not mistaken: TEnum = (two = 2, four = 4).<br>
<br>
---<br>
<br>
IMO FPC/Pascal lacks an assignment operator for enums with range
checking. Something like:<br>
<br>
EnumValue := IntegerValue as TEnum;<br>
<br>
Are there any disadvantages of the enum-AS operator that prevents
its introduction?<br>
<br>
Ondrej<br>
</body>
</html>