<div style="font-family: Arial, sans-serif; font-size: 14px;"></div><span data-olk-copy-source="MailCompose" style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);">Hi all,</span><div style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);">I’d like to start a discussion/gather feedback about a "small" quality of life enhancement to <code><b>java.lang.Enum</b></code> to add a constant-time, allocation-free way to obtain the number of constants in an enum type.</div><div style="text-align: left; margin: 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);"><span>Today, the usual approach is: </span><span style="white-space-collapse:preserve"><i><b>int n = MyEnum.values().length;</b></i></span></div><div style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);">This creates a <b>new array</b> on every call, which is both <b>non-constant time</b> and <b>allocates memory unnecessarily</b>.</div><div style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);"><span>Since the number of enum constants is fixed and known at compile time, this information could be exposed more efficiently.</span></div><div style="text-align: left; margin: 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);"><span>I’ve written a draft JEP describing two possible designs:</span></div><ol data-start="1688" data-end="1863" style="text-align: left; background-color: rgb(255, 255, 255);"><li style="font-family:Calibri, Helvetica, sans-serif;font-size:12pt"><div style="margin:1em 0px">A <b>compiler-generated static constant</b>, e.g.<br><code>public static final int SIZE = <number of constants>;</code></div></li><li style="font-family:Calibri, Helvetica, sans-serif;font-size:12pt"><div style="margin:1em 0px">A <b>built-in API method</b> such as<br><code>MyEnum.size();</code></div></li></ol><div style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);">Draft JEP: <a href="https://gist.github.com/TuranDev/9a20c773d8c698de918da54b2964bf65" title="https://gist.github.com/TuranDev/9a20c773d8c698de918da54b2964bf65" data-linkindex="0" data-auth="NotApplicable" style="margin:0px" target="_blank" rel="noreferrer nofollow noopener">https://gist.github.com/TuranDev/9a20c773d8c698de918da54b2964bf65</a></div><div style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);">I’d appreciate feedback on:</div><ul data-start="1949" data-end="2147" style="text-align: left; background-color: rgb(255, 255, 255);"><li style="font-family:Calibri, Helvetica, sans-serif;font-size:12pt"><div style="margin:1em 0px">Whether this seems like a worthwhile enhancement</div></li><li style="font-family:Calibri, Helvetica, sans-serif;font-size:12pt"><div style="margin:1em 0px">Preference between the two design options</div></li><li style="font-family:Calibri, Helvetica, sans-serif;font-size:12pt"><div style="margin:1em 0px">Whether anyone would be interested in sponsoring the JEP ( or rolling the change into an open piece of work) if there’s agreement to pursue it.</div></li></ul><div style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);">Thank you for your time and thoughts.</div><div style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);">Best regards,</div><span style="text-align: left; margin: 1em 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; background-color: rgb(255, 255, 255);"><b>Turan</b></span><div style="font-family: Arial, sans-serif; font-size: 14px;" class="protonmail_signature_block"><div class="protonmail_signature_block-proton">
    </div>
</div>