Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It means that the type argument for enum has to derive from an enum which itself has the same type argument. How can this happen? By making the type argument the new type itself. So if I've got an enum called StatusCode, it would be equivalent to:</p> <pre><code>public class StatusCode extends Enum&lt;StatusCode&gt; </code></pre> <p>Now if you check the constraints, we've got <code>Enum&lt;StatusCode&gt;</code> - so <code>E=StatusCode</code>. Let's check: does <code>E</code> extend <code>Enum&lt;StatusCode&gt;</code>? Yes! We're okay.</p> <p>You may well be asking yourself what the point of this is :) Well, it means that the API for Enum can refer to itself - for instance, being able to say that <code>Enum&lt;E&gt;</code> implements <code>Comparable&lt;E&gt;</code>. The base class is able to do the comparisons (in the case of enums) but it can make sure that it only compares the right kind of enums with each other. (EDIT: Well, nearly - see the edit at the bottom.)</p> <p>I've used something similar in my C# port of ProtocolBuffers. There are "messages" (immutable) and "builders" (mutable, used to build a message) - and they come as pairs of types. The interfaces involved are:</p> <pre><code>public interface IBuilder&lt;TMessage, TBuilder&gt; where TMessage : IMessage&lt;TMessage, TBuilder&gt; where TBuilder : IBuilder&lt;TMessage, TBuilder&gt; public interface IMessage&lt;TMessage, TBuilder&gt; where TMessage : IMessage&lt;TMessage, TBuilder&gt; where TBuilder : IBuilder&lt;TMessage, TBuilder&gt; </code></pre> <p>This means that from a message you can get an appropriate builder (e.g. to take a copy of a message and change some bits) and from a builder you can get an appropriate message when you've finished building it. It's a good job users of the API don't need to actually care about this though - it's horrendously complicated, and took several iterations to get to where it is.</p> <p>EDIT: Note that this doesn't stop you from creating odd types which use a type argument which itself is okay, but which isn't the same type. The purpose is to give benefits in the <em>right</em> case rather than protect you from the <em>wrong</em> case.</p> <p>So if <code>Enum</code> weren't handled "specially" in Java anyway, you could (as noted in comments) create the following types:</p> <pre><code>public class First extends Enum&lt;First&gt; {} public class Second extends Enum&lt;First&gt; {} </code></pre> <p><code>Second</code> would implement <code>Comparable&lt;First&gt;</code> rather than <code>Comparable&lt;Second&gt;</code>... but <code>First</code> itself would be fine.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload