score:10
I don't know if this is intended or would be considered a bug, but here is what I think is happening.
In def foo: Option[Wrapper[String]] = Some("foo")
the compiler will set the expected type of the argument provided to Some( )
as Wrapper[String]
. Then it sees that you provided a String
which it is not what is expected, so it looks for an implicit conversion String => Wrapper[String]
, can't find one, and fails.
Why does it need that expected type stuff, and doesn't just type Some("foo")
as Some[String]
and afterwards try to find a conversion?
Because scalac wants to be able to typecheck the following code:
case class Invariant[T](t: T)
val a: Invariant[Any] = Invariant("s")
In order for this code to work, the compiler can't just type Invariant("s")
as Invariant[String]
because then compilation will fail as Invariant[String]
is not a subtype of Invariant[Any]
. The compiler needs to set the expected type of "s"
to Any
so that it can see that "s"
is an instance of Any
before it's too late.
In order for both this code and your code to work out correctly, I think the compiler would need some kind of backtracking logic which it doesn't seem to have, perhaps for good reasons.
The reason that your Working
code does work, is that this kind of type inference does not span multiple lines. Analogously val a: Invariant[Any] = {val why = Invariant("s"); why}
does not compile.
Source: stackoverflow.com
Related Query
- Implicit conversions weirdness
- How can I get implicit conversions to work inside collections?
- Can someone explain me implicit conversions in Scala?
- Question about Scala implicit conversions Non-Ambiguity Rule
- A problem of implicit conversions in scala 2.8
- Implicit conversions and null
- Implicit conversions for defs/lambdas in Scala?
- Why these implicit conversions resulted in looping code
- Resolving ambiguous implicit conversions in Scala
- Implicit conversions in the context of a (case) class constructor
- Scala: Importing implicit conversions in all subpackages of some package
- Find implicit conversions
- Scala: Multiple implicit conversions with same name
- Why cannot this case of implicit conversions be optimized?
- Overriding arithmetic operators on Int via implicit conversions
- Why do we have to explicitly import implicit conversions having implicit parameters from companion objects? Strange.
- Ambiguous implicit conversions causing compile failure in Scalatest and Argonaut.io
- List of String implicit conversions like +=
- Can Scala apply multiple implicit conversions in one expression?
- import implicit conversions without instance of SparkSession
- How can collections use implicit conversions on element types?
- Chaining implicit conversions of function to a generic class
- Using method name "x" with implicit conversions in Scala
- Implicit conversions causing infinite recursion, but that shouldn't typecheck
- Weird Scala bug related to implicit conversions on default constructor parameters
- Implicit conversions for members that are types
- Scala implicit conversions between functional objects?
- Pimp my function in scala - applying implicit conversions on functions
- Should I use implicit conversions to enforce preconditions?
- Scala delegate import of implicit conversions
More Query from same tag
- Get random number between two numbers in Scala
- How to reduce code repetition on AWS Deequ
- Want to change the date format from Timestamp to DateTime format
- Type mismatch on value class with value field and imported cats either syntax
- Controller POST action responds "Unauthorized"
- Spark Standalone
- How to save a two-dimensional array into HDFS in spark?
- Scala - distinct on list, but with a pick order / priority
- Replicating rows in Spark dataframe according values in a column
- How to eliminate vars in a Scala class?
- Launching and reloading Jetty from within IntelliJ IDEA (Scala, SBT)
- Optional Json Body Parser
- Any performance impact with Java modules used in Scala programs?
- Spark SQL(v2.0) UDAF in Scala returns empty string
- Scala Swing Caret Position
- How to define a trait with methods accepting any subtype of a particular trait
- spark-submit for a .scala file
- An umbrella Scala project. How to create multimodules project with multiple versions of scala, and libs?
- Regex on Spark RDD[String] with Regex on multiline
- How do I change the way an inspection works in IntelliJ IDEA?(Scala plugin)
- when to use singleton objects and when to use actual objects in scala
- IntelliJ gives errors saying "Cannot resolve symbol " in build.sbt after creating Scala project
- Sum of n numbers result is wrong in Scala
- Apple postion in snake game scala
- Scala println in a for loop
- How to set Span Context in current Context?
- Using Kamon to generate a TraceContext in Spray
- Changing Scalatra Port
- Scala create array of empty arrays
- Akka Remote Actors- : can not able to establish a connection between local and remote actors