score:1
i don't have an answer per say, but here are some thoughts. first of all, i've simplified and clarified your code example to minimize down to the problem:
trait effect[a]
trait actionresult[+m, +a]
trait modelupdated[+m] extends actionresult[m, nothing]
trait haseffect[+m, +a] extends actionresult[m, a]
case class modelupdateeffect[m, a](newmodel: m, effect: effect[a]) extends modelupdated[m] with haseffect[m, a]
object main {
def foo[m, a]: actionresult[m, a] = ???
def dispatch[m, a] = {
foo[m, a] match {
case modelupdateeffect(newmodel, effect) =>
val e: effect[a] = effect // does not compile
false
case _ => true
}
}
}
let's note the error message we get for that line:
type mismatch;
found : effect[any]
required: effect[a]
note: any >: a, but trait effect is invariant in type a.
it's kind of strange the compiler decides that effect
has type effect[any]
. but let's see what happens if we replace this definition:
case class modelupdateeffect[m, a](newmodel: m, effect: effect[a]) extends modelupdated[m] with haseffect[m, a]
with this:
case class modelupdateeffect[m, a](newmodel: m, effect: effect[a]) extends haseffect[m, a]
now we get a different error message:
type mismatch;
found : effect[?a1] where type ?a1 <: a (this is a gadt skolem)
required: effect[a]
note: ?a1 <: a, but trait effect is invariant in type a.
in this case, the types really don't match up right. let's walk through it. we know from outside the case
statement that we have an actionresult[m, a]
. but because of the covariance on the type param a
, that actionresult[m, a]
may well be an actionresult[m, b] forsome { type b <: a }
. in other words, there may be some type b
that is a sub-type of a
, and foo[m, a]
might return an actionresult[m, b]
. in which case, effect
would be an effect[b]
, and because the type parameter for effect
is invariant, this type is not compatible with effect[a]
.
Source: stackoverflow.com
Related Query
- Type Bounds and Pattern Matching in Scala
- Pattern matching loses type bounds?
- type parameter bounds compiler error while pattern matching in Scala
- Scala multiple type pattern matching
- Pattern matching on generic type in Scala
- Pattern matching against Scala Map type
- Type mismatch on abstract type used in pattern matching
- Difference between type inference of method and class type parameters in pattern matching
- Cake pattern with overriding abstract type don't work with Upper Type Bounds
- Scala compiler cannot infer mix-in type for pattern matching
- Pattern matching on List[T] and Set[T] in Scala vs. Haskell: effects of type erasure
- Pattern matching on classes containing generalized type constraints
- Scala type erasure in pattern matching Map[String, Int]
- Scala pattern matching on generic type with TypeTag generates a warning while ClassTag not?
- Scala type erasure for pattern matching
- Scala pattern matching and type inference
- Scala type erasure in pattern matching Map[String, Any]
- What's the relationship between pattern matching and type system?
- Scala: pattern matching on nested type hierarchies
- type level pattern matching in scala
- Why does Scala not infer the type parameters when pattern matching with @
- Context bounds and pattern matching in scala
- Pattern matching on a shapeless type cast ok?
- Incorrect type inference while pattern matching in Scala
- Type pattern matching and inference error in Scala 3
- Retrieve type parameter in pattern matching in scala
- Type to capture either integer, float or a string value and then do pattern matching in Scala
- Scala pattern matching with Option type
- Scala Number type pattern matching
- Matching type of head/tail pattern match argument
More Query from same tag
- What happens to the scheduler when an actor system shuts down?
- Locally working web-app throws VerifyError on GAE
- How do I make Codec[Option[T]] that is driven by prefix?
- Skip/Take with Spark SQL
- find set of keys in Scala map where values overlap
- How to get node information on Spark Decision Tree model
- Using scala template code blocks and values as argument to template call (Play Framework)
- How can I count number of rows returned by a join in Slick?
- Is there a tool for Scala to clean all the unused imports from all the code files?
- Getting Value of Either
- How to convert Seq[Object] to Map[User, Set[String] in Scala
- How to pass differnt filenames to spark using scala
- Scala: Dealing with optional XML elements and Case Classes
- Can't take configuration value in view at 2.5.3 version?
- why can not overload no argument method ,for implicit class
- Is it possible to build type-driven function lookup tables in Scala?
- How to set redirect strategy in apache async http client
- Cast Numeric to Float in Scala
- Inheritance in Lift Mapper or Record Framework
- Improving ugly parameter list on traits
- Trying to write a generic filter function in Scala
- Play runtime DI disadvantages
- scala Error handling inside a function
- Scala Spark collect_list() vs array()
- sbt not generating bin directory when staging or publishing docker + other questions
- While in scala REPL, can I execute external script from file?
- Loop through list to call different functions
- Scala: Trait Mixin with Abstract Base Class
- Scala typing: "Expected _$1, got T"
- Vertex Property Inheritance - Graphx Scala Spark