I'm trying to implement generic functions working on different type of vector which can be real, binary, and mixt. Mixt are wrapped inside a class composed of both real and binary part.
import spire.math.{Numeric => SNumeric}
// basic function working on any kind of vectors
def f[O, D <: Distance[O]](data: GenSeq[O], metric: D) = ...
// function working on real and binary vectors
def f[@specialized(Int, Double) N: SNumeric, V[N] <: Seq[N], D <: DistanceSeq[N, V[N]]](data: GenSeq[V[N]], metric: D) = ...
where
// general distance definition
trait Distance[O] extends Serializable {
def d(o1: O, o2: O): Double
}
// Real and binary distance definitions
trait DistanceSeq[@specialized(Int, Double) N, V <: Seq[N]] extends Distance[V]
Does f
and DistanceSeq
specializations effectively speed up methods applications when they are called ? Is there a better way to implement it ?
I also would like to know if i'm calling f
without the corresponding generic arguments does it automatically select the much specialized method or do i need to specify generic arguments in order to it takes the best one.