This is tangentially related to Why doesn't Java 8's ToIntFunction extend Function<T, Integer>
I find it mildly annoying that the primitive versions of Optional
and various functional interfaces use different method names, like getAsInt()
instead of get()
and applyAsDouble(x, y)
instead of apply(x, y)
. I wondered why they didn't just go with the same method names. After all, all of the streams use map
, filter
, reduce
, etc, regardless of type, which seems to make perfect sense. There doesn't seem to be a point in having doubles.filterDoubles(DoublePredicate)
and ints.reduceAsInt(IntBinaryOperator)
. But then why would we have to do ints.reduce(Integer::sum).getAsInt()
?
In particular, it's interesting to note the asymmetry. Both ints.reduce(...).get()
and ints.reducePrimitive(...).getAsPrimitive()
would be consistent. Yet we have neither.
One reason I could think of was wanting to make inheritance possible. If we wanted an OptionalInt
to also be an Optional<Integer>
, then we couldn't have both an int get()
and an Integer get()
method. So then it would make sense to extend Optional<Integer>
, inherit the autoboxing method, and then also add an additional primitive-only method for avoiding the autoboxing for performance. Same idea goes for functions. But as the question above discusses, the primitive functional interfaces do not extend the generic ones, and neither do the primitive Optional classes.
Is there something I'm not seeing? Would using get
and apply
break something? Or is this just an accidental inconsistency in the API, analogous to String.length()
vs List.size()
? I suppose it's possible that the designers wanted to leave the door open to using inheritance as described above in the future. Can anyone think of another reason?