score:7
After the compiler handles named and default parameters, the calls become
foo.copy("foo1.1", foo.name, foo.v1)
and
foo.copy("foo1.1", foo.name, foo.v1, foo.v2)
respectively. Or, if you replace the parameters with types,
foo.copy[?](String, String, Bar[_])
and
foo.copy[?](String, String, Bar[_], Bar[_])
?
is the type parameter of copy
which has to inferred. In the first case the compiler basically says "?
is the type parameter of Bar[_]
, even if I don't know what that is".
In the second case the type parameters of two Bar[_]
must really be the same, but that information is lost by the time the compiler is inferring ?
; they are just Bar[_]
, and not something like Bar[foo's unknown type parameter]
. So e.g. "?
is the type parameter of first Bar[_]
, even if I don't know what that is" won't work because so far as the compiler knows, the second Bar[_]
could be different.
It isn't a bug in the sense that it follows the language specification; and changing the specification to allow this would take significant effort and make both it and the compiler more complicated. It may not be a good trade-off for such a relatively rare case.
Another workaround is to use type variable pattern to temporarily give a name to _
:
foo match { case foo: Foo[a] => foo.copy(id = "foo1.1") }
The compiler now sees that foo.v1
and foo.v2
are both Bar[a]
and so the result of copy
is Foo[a]
. After leaving the case
branch it becomes Foo[_]
.
Source: stackoverflow.com
Related Query
- Scala case class copy doesn't always work with `_` existential type
- Scala copy case class with generic type
- Problem with bounded type parameterised case class and default args in Scala
- Scala case class copy constructor with dynamic fields
- Scala case class copy with optional values
- Scala type inference not working with generic case class and lambda
- Scala case class copy with dynamic named parameter
- Scala case class copy only with some parameters at runtime?
- How to partially apply case class with type parameter in Scala
- Automatic type class derivation for case classes with Scala Enumeration fields
- Scala macro annotation - case class with type parameters
- Change fields of case class based on map entries with simple type check in scala macros
- Scala case class copy method with Map parameter
- Is there a way to guarantee case class copy methods exist with type classes in Scala?
- scala type error for class with map member typed with existential type
- Scala case class with argument type change at runtime
- Scala - copy case class with dynamic fields. Is it possible?
- Scala Path Dependant Return Type Case Class Copy
- Why Scala case class copy method parameterised only with the variables defined in the case class?
- Pattern matching with case classes with type parameter doesnt work
- How to encode scala case class to json with any as parameter type of case class?
- Scala case class member parameterized with wildcard types does not infer type bound when called
- Create "enriched" type from case class with Shapeless in Scala
- How to update a mongo record using Rogue with MongoCaseClassField when case class contains a scala Enumeration
- Scala case class extending Product with Serializable
- case class copy 'method' with superclass
- Read CSV in Scala into case class instances with error handling
- Type aliasing a case class in Scala 2.10
- scala 2.10.2 calling a 'macro method' with generic type not work
- Scala wont pattern match with java.lang.String and Case Class
More Query from same tag
- SparkStreaming/Scala/Twitter java.lang.NoSuchMethodError: twitter4j.TwitterStream.addListener(Ltwitter4j/StreamListener;)V
- how to call scala REPL console from scala program?
- Error when trying to write to hdfs: Server IPC version 9 cannot communicate with client version 4
- Error while using truncateTable method of JDBCUtils in PostgreTable using Spark
- How to import play in scala repl
- LDAP for authentication and authorization in Playframework 2.0
- Ending a for-comprehension loop when a check on one of the items returns false
- Message Class in Akka: Sending an array/buffer as a message
- How to get the name of the current scalajs / uTest test case
- Scala: constructor with two paretneheses
- How do I set up jsr223 scripting with scala as scripting language
- Named parameters vs _, dot notation vs infix operation, curly vs round brackets when using higher-order functions in Scala
- scala Map filterKeys: Projection cannot be assigned to a Map reference
- Scala- Databricks- Linear Regression
- How do I do casting in Scala?
- Efficiently find common values in a map of lists - scala
- Redirecting to search params in lift
- How come I can define generic exception types in Scala?
- How to define generic type in Scala?
- Unmarshalling error onFailure from http POST with spray.client
- How to split the spark dataframe into 2 using ratio given in terms of months and the unix epoch column?
- scalatest intercept vs a[Exception] should be thrownBy
- How can I use scala sources from a different location in a sbt project and also make it work with IntelliJ IDEA?
- Spark Scala Get Data Back from rdd.foreachPartition
- How to handle JsValue in Scala/Play
- How does $(objectName) work in Scala Spark?
- Playframework Scala Authentication Builder Cached onUnauthorized
- Scala sbt unable to find commons-logging-1.0.4.jar
- Unresolved dependency: sbt-plugin;2.7.0 when trying to compile a play template project
- Using callUDF to create a method that chains UDF calls