<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><div data-crea="font-wrapper" style="font-family: Tahoma; font-size: 16px; direction: ltr"><div style="font-family: Tahoma; font-size: 16px"></div><div data-anchor="reply-title">Am Sa., Jul. 15, 2017 12:45 schrieb Jonas Maebe <jonas@freepascal.org>:</div><blockquote><div>Classes are explicitly documented to initialise their contents with <br>zero</div></blockquote><span><div><span><br></span></div><div><span>Excuse me, but you have a collision in the way you think :)</span></div><div><span><br></span></div><div><span>On one hand, you try to be very fundamental about enums - you say that only declared enum values are valid. And t</span>here is no zero value for <font face="Helvetica, Tahoma, Arial, sans-serif"><span style="font-size: 14.6667px;">TMyEnum. </span></font><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;">TMyEnum is declared as follows:</span></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;"><br></span></div><div><font face="Helvetica, Tahoma, Arial, sans-serif"><span style="font-size: 14.6667px;">TMyEnum = (one = 1, two);</span></font><br></div><div><font face="Helvetica, Tahoma, Arial, sans-serif"><span style="font-size: 14.6667px;"><br></span></font></div><div><font face="Helvetica, Tahoma, Arial, sans-serif"><span style="font-size: 14.6667px;">TMyEnum is not a number so it cannot be initialized to zero. Because there is no zero. There are only two values, "one" and "two". I don't care about the bit pattern of the enum value - this is an implementation detail for me (that could change in the future just the same as the CASE statement changed).</span></font></div><div><font face="Helvetica, Tahoma, Arial, sans-serif"><span style="font-size: 14.6667px;"><br></span></font></div><div><font face="Helvetica, Tahoma, Arial, sans-serif"><span style="font-size: 14.6667px;">On the other hand you say, it is documented to be declared to zero. So you say that an enumeration is an integer value with aliases for number values.</span></font></div><div><font face="Helvetica, Tahoma, Arial, sans-serif"><span style="font-size: 14.6667px;"><br></span></font></div><div><font face="Helvetica, Tahoma, Arial, sans-serif"><span style="font-size: 14.6667px;">Well, you have 2 ways of solving this:</span></font></div><div><font face="Helvetica, Tahoma, Arial, sans-serif"><span style="font-size: 14.6667px;"><br></span></font></div><div><font face="Helvetica, Tahoma, Arial, sans-serif"><span style="font-size: 14.6667px;">1.) HIGH-LEVEL enumeration: You say that an enumeration can have a value from a strict </span></font><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;">set of identifiers. In this case, you have to handle it like this in all cases:</span></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;"><br></span></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;">1a) EnumValue := TEnum(IntegerValue) has to assign an always valid value to </span><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;">EnumValue (without range checking) or has to raise a range check error (with range checking) if IntegerValue is not allowed in TEnum. It should be the same like when you assign an Int64 value to Integer field - you always get a valid integer (without range cheching) !!!<br>1b) You have to initialize EnumValue in objects to valid values, whatever the bit pattern is (bit pattern is not in high-level enumerations).</span></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;">1c) Inc/Dec has to increase/decrease the enum values and not ordinal values:</span></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;">TEnum = (zero, two=2);</span></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;">MyEnum := zero;</span></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;">Inc(MyEnum); -> has to increase </span><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;">MyEnum to</span><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;"> two, not 1.</span></div></span><span><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;">1d) In this case you can leave the CASE optimization as it is.</span></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;"><br></span></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;">- OR -</span></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;"><br></span></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;">2.) LOW-LEVEL enumeration: You say that an enumeration is an ordinal type with enumeration values being only aliases for underlying ordinal values. An enumeration can have any possible value that is allowed by the ordinal type (Byte, Word, whatever).</span></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;">=> 1a) + 1b) + 1c) + 1d) are not valid any more.</span></div><div><br></div></span><span><div><font face="Helvetica, Tahoma, Arial, sans-serif"><span style="font-size: 14.6667px;">-----</span></font></div><div><font face="Helvetica, Tahoma, Arial, sans-serif"><span style="font-size: 14.6667px;"><br></span></font></div><div><font face="Helvetica, Tahoma, Arial, sans-serif"><span style="font-size: 14.6667px;">Conclusion: you did only 1d - so you have done only 1 point from 4 (I may have forgotten some). All in all, the </span></font><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;">HIGH-LEVEL enumeration approach cannot be used in Pascal at all (you cannot fix 1a-1c) - because of code speed and/or compatibility reasons.</span></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;"><br></span></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;">So, IMO the HIGH-LEVEL enum approach is wrong along with the CASE optimization.</span></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;"><br></span></div><div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14.6667px;">I understand what you say about validity of enum values - but you did only the CASE optimization, not other steps that are from the same pot (1a-1c). If you want to leave the CASE optimization, you have to fundamentally change the enum type and fix 1a-1c.</span></div><div><br></div><div> <br></div></span><blockquote><div>That is also why we warn when you use a local ansistring variable <br>without initialising it first: even though it won't crash your program <br>(due to underlying needs by the reference counting mechanism), it is <br>still a logic error.</div></blockquote><span><div><span><br></span></div>Local variables are off-topic.</span><div><span><br></span></div><div><span>Ondrej </span><br><blockquote><div></div></blockquote></div></div></body></html>