2

Why has java decided to throw an ArrayStoreException in the following method <A> A[] Stream<T>.toArray(IntFunction<A[]>) if the runtime type of any of the stream elements isn't a subtype of the runtime type of the array.

Instead, if the generic type A was bounded like in the following <A super T> A[] Stream<T>.toArray (IntFunction<A[]>) this will remove the necessity of throwing the exception above and being checked at compile time.

marsouf
  • 1,067
  • 7
  • 14
  • 1
    [That signature is not possible in java, and if it were, it wouldn't do what you'd want here](https://stackoverflow.com/a/2800425/8491228) – Mor A. Aug 28 '17 at 20:09
  • @M.Aroosi thank you, that's what I thought of. – marsouf Aug 28 '17 at 20:13
  • Make sure to read the second answer, because the first one has some important deficiencies due to conflating the problems of array covariance with the problems of `super` bounds. – user2357112 Aug 28 '17 at 20:14

0 Answers0