scalaimplicitimplicit-methods

Recursively using implicit methods in Scala


I'd like to define some implicit methods for arrays of doubles 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 doesn't work as written because Scala won't apply implicit methods recursively. Specifically, I get an error Note: implicit method enrichVec is not applicable here because it comes after the application point and it lacks an explicit result type. I could avoid this by explicitly writing out the code for normalize, but that would be ugly. Is there a nicer solution?


Solution

  • The anonymous class inhibits the recursive function definitions. You need to define the "RichVec" as a class and then separately define the implicit conversion.

    type Vec = Array[Double]
    implicit def enrichVec(v: Vec) = RichVec( v )
    case class RichVec( v: Vec ) {
      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 )
    }