<div dir="ltr"><div dir="ltr">On Sat, Jul 13, 2019 at 2:37 PM Ondrej Pokorny <<a href="mailto:lazarus@kluug.net">lazarus@kluug.net</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
I do exactly the same - check the low/high bounds in a type helper :) <br>
Yes, and I am tired of typing it as well :)<br></blockquote><div><br></div><div>You can pretty easily write a generic function that will work on pretty much any enum for this, BTW:</div><div><br></div>program Example;<br><br>{$mode Delphi}<br><br>uses TypInfo, SysUtils;<br><br>function TryConvert<T>(const Value: SizeInt): T; inline;<br>begin<br> if (Value >= SizeInt(Low(T))) and (Value <= SizeInt(High(T))) then<br> Result := T(Value)<br> else<br> raise EConvertError.Create(<br> Format(<br> 'Invalid value for %s : %d', [<br> PTypeInfo(TypeInfo(T))^.Name,<br> Value<br> ]<br> )<br> );<br>end;<br><br>type<br> Letter = (A, B, C);<br><br>begin<br> WriteLn(TryConvert<Letter>(0));<br> WriteLn(TryConvert<Letter>(1));<br> WriteLn(TryConvert<Letter>(2));</div><div class="gmail_quote"> // Exception!<br> WriteLn(TryConvert<Letter>(3));<br><div>end.</div><div><br></div><div>The exception printing "EConvertError: Invalid value for Letter : 3", of course.</div></div></div>