I would like to define some implicit methods for double arrays to make my code cleaner. Ideally, they would look like this:
type Vec = Array[Double] implicit def enrichVec(v: Vec) = new { def /(x: Double) = v map (_/x) def *(u: Vec) = (v zip u) map {case (x,y) => x*y} sum def normalize = v / math.sqrt(v * v) }
However, the normalize
function does not work as it is written, because Scala will not use implicit methods recursively. In particular, I receive the error message Note: implicit method enrichVec is not applicable here because it comes after the application point and it lacks an explicit result type
. I could have avoided this by explicitly writing code for normalize
, but that would be ugly. Is there a nicer solution?
source share