score:9
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
x.toDouble
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(_) }
Source: stackoverflow.com
Related Query
- scala implicit causes StackOverflowError
- Scala Macro Annotations: c.TypeCheck of annotated type causes StackOverflowError
- Understanding implicit in Scala
- Defining a function with multiple implicit arguments in Scala
- Is it possible in Intellij IDEA Scala plugin to know which implicit conversion was applied?
- What is the performance impact of Scala implicit type conversions?
- Implicit keyword before a parameter in anonymous function in Scala
- Scala implicit usage choices
- StackOverflowError with Scala on IntelliJ
- Scala dependency injection: alternatives to implicit parameters
- Scala implicit object vs implicit val
- Defining implicit view-bounds on Scala traits
- Avoiding implicit def ambiguity in Scala
- Play 2.3 implicit json conversion causes null pointer exception
- Implicit conversion from String to Int in scala 2.8
- Cannot find an implicit ExecutionContext. You might pass spray scala
- Why is currying and uncurrying not implicit in scala
- Scala implicitly vs implicit arguments
- Play Scala No Json deserializer found for type (String, String). Try to implement an implicit Reads or Format for this type
- Scala implicit conversion from parent trait
- How to use Scala 2.10 implicit classes
- scala implicit or explicit conversion from iterator to iterable
- How to stub a method call with an implicit matcher in Mockito and Scala
- Scala Implicit Ordering
- Implicit conversion from Int to Double in scala doesn't work
- Scala testing mocking implicit parameters?
- Implicit conversion of java.util.List to scala List does not occur
- scala cats ambiguous implicit values
- Scala named and default arguments in conjunction with implicit parameters
- Scala - Co/Contra-Variance as applied to implicit parameter selection
More Query from same tag
- What does this self-type mean?
- Delta Lake: File Not Found Exception
- Iterate two collections in one loop
- scala.collection.immutable.$colon$colon cannot be cast to java.lang.String
- AWS Glue : Unable to process data from multiple sources S3 bucket and postgreSQL db with AWS Glue using Scala-Spark
- export Intellij Idea scala static analysis rules into a scalastyle_configuration file
- Best language for Molecular Dynamics Simulator, to be run in production. (Python+Numpy?)
- Using Apache Spark in IntelliJ Scala Worksheet
- Finding type parameters via reflection in Scala 2.10?
- Exception from sonarqube scan when using using kafka (or scala in general?)
- Flatten only deepest level in scala spark dataframe
- Why does my "Try" always fall into the "Success" when trying to match?
- Pass system property to spark-submit and read file from classpath or custom path
- Abstract classes with constructor parameters in Scala
- Scala existentials placeholder translation for M[_,_] where M[X,Y <: N[X]]
- Type parameter that contains type parameter itself
- Writing data to cassandra using spark
- What if there is no match type?
- Preferred way of grouping utility functions in Scala?
- Implicit resolution failure on import of implicit value with same name as method in class
- Scala pattern matching aggregate multiple matches
- Spark columnar performance
- Spark scala - calculating dynamic timestamp interval
- Read CSV File in Spark and Write it to Cassandra
- Scala Mock Syntax (class _).expects meaning?
- Scala How To Send Anonymous Functions Results as Argument
- NATS streaming server subscriber rate limiting and exactly once delivery
- XML Parsing in Databricks Spark Scala AWS using MAVEN - HL7 V3 files from DailyMed
- How to extend class defined in one scala file from another scala file
- Obtain asyncronous result from listener