I have a third party Java API with methods like this (simplified) :
public <C extends Comparable<C>> C jFoo(C c);
I want to write a wrapper like this:
def foo[C <: Comparable[C]](c: C): C = jFoo(c)
For allowing the wrapper method to accept primitives (since, to my understanding, boxing will happen anyway so that's not relevant), I want to specialize C
:
def foo[@specialized(Int, Double) C <: Comparable[C]](c: C): C = jFoo(c)
However, scalac complains that, due to the upper bound on the parameter, the method cannot be specialized.
A solution would be to add an evidence parameter:
def foo[@specialized(Int, Double) C, CC <: Comparable[CC]](c: C)
(implicit evComp: C => CC): CC =
jFoo(evComp(c))
This works, and you can now call:
foo(2.0)
However, the method signature - part of the API - is not really readable now.
Is there an alternative approach at the method level that would produce an equivalent, more readable signature, while maintaining specialization?
(Scala 2.12.x is fine, Dotty not so much)