Generic specialization along with value types are a projected feature of future JVMs; link to the Valhalla project page here.
Now, from what I understand, it would then become possible to declare a:
final List<int> myList = new ArrayList<>(); // for instance
But then List
defines another .remove()
method in addition to the one defined in the Collection
interface, which takes an int
as an argument which is the index in the list to remove; that is why, currently, the content of list
in the example below:
final List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.remove(2);
will be [1, 2]
and not [1, 3]
(the most specific overload is chosen).
However, if in the future we are able to declare a List<int>
, we have a problem: what overload of the remove
method will be chosen?
This answer is based on this paper by Brian Goetz, dated December 2014. This is the latest I could find on the subject; note however that the paper is an "informal sketch" so there's nothing definitive yet regarding your question.
First, a
List<int>
would not be a subtype ofList<Integer>
(Subtyping):This paper also lists "Migration challenges", and reference-primitive overloadings (the problem is your question) is one of them:
A proposed solution is refered to as the "peeling" technique:
With this technique, backward compatibility would be kept and the new methods
removeByValue
andremoveByIndex
would be used.