Accepted answer

I believe I can answer what's happening here.. it's related to other implicit conversions, and the one you just created. If you add this trace you can confirm what stack overflow usually relates to - a function calling itself repeatedly until the stack space of java crashes:

implicit val stringsToDouble: String => Double= { x=>println("called inner "+x); x.toDouble }

.... called inner 4.5 called inner 4.5 called inner 4.5 called inner 4.5 called inner 4.5ERROR: java.lang.StackOverflowError

I think what's happening is this - toDouble is not a natural function of java string, but rather happens using an Implicit conversion in StringOps (or StringLike, I'm not really sure but it's the same issue).

So when you call toDouble - the compiler starts seeking an implicit conversion that could contain the function "toDouble". In theory it could be any resulting class.

BUT - what should happen if several implicit conversions could achieve this? Unfortunately, "Double" also contains the function toDouble as proven here:

val x = 44.4

And guess what? That means your new implicit function, now closest in scope wins the contest and get's called in a circle to accomplish "toDouble" - effectively trying to turn the string into a double, in order to call toDouble (on the class Double), repeatedly.. I'll admit it's fairly confusing, but the evidence fits.

Here's the fix.. it fits the explanation and prevents the recursive calls.

 implicit val stringsToDouble: String => Double= { java.lang.Double.parseDouble(_)  }

Related Query

More Query from same tag