IntelliJ Annotation Inference - Infers narrow contracts where a wide one is desired
I use IntelliJ as a primary IDE for writing Java code.
In this code I write a number of "Value-based types". In writing this code, I've noticed that some annotations IntelliJ infers about my code are much stricter than I would want the public api to express. In particular, when writing the factory methods of such value-based types, I typically just have a naive implementation that returns a new object, and, as a result, IntelliJ infers that the contract is `_ -> new`. However, this is an incorrect contract, for the reasons detailed below.
Value-based types are called that, because the only identity objects of the type should consider, is the publically observable state. In particular, factory methods may be implemented more efficiently than simply calling a constructor, and, because all constructors are private, you should never consider the object's identity (and in fact, in future releases of java, the inline classes feature worked on, whenever that becomes part of the language, and objects of inline classes have no identity).
However, this infered contract would seem to imply that repeated calls to the method compare unequal. Thus the IDE is inferring facts that may not hold true for future versions. This seems to be a forward compatibility pit fall, especially for api writers using the semantic versioning scheme.
Would it be reasonable to reduce the scope of annotation inference? Particularily, there should not be any narrow contract inference about return values from public api functions, as it being inferred wrong and taken as a fact would not be forward compatible. Parameter inference is generally fine, though, as widening a parameter contract is acceptable as a minor change. However, widening a return contract is a major breaking change.
Please sign in to leave a comment.
Please submit a new bug at https://youtrack.jetbrains.com/newIssue?project=IDEA with a little sample project attached.