Recursively using implicit methods in Scala

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?

+4
source share
2 answers

An anonymous class prohibits the definition of recursive functions. You must define "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 ) } 
+6
source

It works:

 type Vec = Array[Double] abstract class RichVec(v: Vec) { def /(x: Double): Vec def *(u: Vec): Double def normalize: Vec } implicit def enrichVec(v: Vec): RichVec = new RichVec( v ) { 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) } 

But there are other ways to do this. The most important thing is that you specify the return type for the implicit.

0
source

Source: https://habr.com/ru/post/1383790/


All Articles