Guava: Consider tweaking the `Comparators#{emptiesFirst,emptiesLast}` method signatures

Created on 1 Jan 2018  路  2Comments  路  Source: google/guava

Guava 22.0 introduced Comparators#emptiesFirst(Comparator<T>) and Comparators#emptiesLast(Comparator<T>). Since these methods are still in @Beta, can their signatures be updated to accept a Comparator<? super T>, just like #least and #greatest, as well as various methods defined in java.util.Comparators?

Rationale: as it stands my compiler complains when I write emptiesFirst(naturalOrder()):

The method emptiesFirst(Comparator<T>) in the type Comparators is not applicable for the arguments (Comparator<Comparable<? super Comparable<? super T>>>).

The workaround is to provide an explicit type parameter to either emptiesFirst or naturalOrder, but if the signatures are updated as suggested above that won't be necessary.

package=collect status=fixed type=defect

Most helpful comment

And sure enough, most internal callers have to provide an explicit type parameter. Yuck.

All 2 comments

I think we should do this.

We don't often use "loose" generics, but here there's more reason to, as you explain. I know that we already use them in Ordering: nullsFirst/nullsLast, reverse, lexicographical. And in fact we carried that over to Comparators when copying lexicographical there. And our original API Review doc for emptiesFirst/emptiesLast, back when we were considering adding it to Ordering itself, says "(Technically this would use <S extends T> as does nullsFirst(), but that's an insignificant detail.)" So it appears that we even intended to do this, unless something is different about the static methods than the instance methods. But the Comparators.lexicographical signature suggests we consider them to be the same. (Internal discussion on CL 122475150.)

I'm going to make sure it doesn't break anything internally. Assuming not, I'll make the change. Thanks for bringing this up.

And sure enough, most internal callers have to provide an explicit type parameter. Yuck.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thecoop picture thecoop  路  4Comments

edwardlee03 picture edwardlee03  路  4Comments

PhilippWendler picture PhilippWendler  路  4Comments

Hanmac picture Hanmac  路  3Comments

Bocete picture Bocete  路  3Comments