score:5
Accepted answer
seq(1, 2, 3)
has type seq[int]
, seq("1", "2", "3")
has type seq[string]
. both seq[int]
and seq[string]
are subtypes of seq[any]
. so list(seq(1, 2, 3), seq("1", "2", "3"))
has type list[seq[any]]
.
if you want types seq[int]
and seq[string]
to be preserved then you need not a list
but hlist
import shapeless.{hlist, hnil}
import shapeless.ops.hlist.liftall
def f[l <: hlist](seq: l)(implicit ev: liftall[({type l[t] = or[t =:= seq[int], t =:= seq[string]]})#l, l]) = {
println(seq)
}
f(seq(1, 2, 3) :: seq("1", "2", "3") :: hnil)
Source: stackoverflow.com
Related Query
- Why Does This Type Constraint Fail for List[Seq[AnyVal or String]]
- Why does the type parameter bound T <: Comparable[T] fail for T = Int?
- Why does this Scala for expression using tuples fail to compile?
- Why does Scala implicit resolution fail for overloaded method with type parameter?
- Why does the Scala compiler fail with missing parameter type for filter with JavaSparkContext?
- Why does Scala's type inferencer fail with this set of implicit arguments involving parameterized types?
- Why does reading stream from Kafka fail with "Unable to find encoder for type stored in a Dataset"?
- Why does creating a DataFrame fail with "java.lang.UnsupportedOperationException: Schema for type Category is not supported"
- Why does Spark fail with "Detected cartesian product for INNER join between logical plans"?
- Why does Scala require a return type for recursive functions?
- Why does 2.11.1 fail with error: not found: type Application?
- Why does this Scala function compile when the argument does not conform to the type constraint?
- Why does Scala choose the type 'Product' for 'for' expressions involving Either and value definitions
- Why does Scala type inference fail here?
- Why does this not give a type error?
- Why scala's pattern maching does not work in for loops for type matching?
- Why does Scala fail to find a secondary implicit value in this one particular case?
- Why scala does not unify this type lambda with underlying type?
- Why does this getOrElse statement return type ANY?
- Why this structural type bound does not work as expected?
- Why does this two checks for null and empty returns different results?
- Why do I need an explicit evidence type / why does this Scala type bound fail?
- Why does scala compiler fail to find implicit parameter value/conversion when it is an overload and has generic type param?
- Why does Scala 3 fail to reduce an inline match over Either in this example?
- Why does this type alias not compile (Scala)
- Why does SonarQube find this issue (<static initializer for >() uses a Side Effect Constructor) with case and object class files?
- Why does this Scala implicit conversion fail when explicit conversion works?
- Why this simple Scala for comprehension does not execute the futures?
- Why does sbt fail to run a gui application for the second time?
- Why does this json4s code work in the scala repl but fail to compile?
More Query from same tag
- How to filter on criteria based on tags in Scala
- Conflicting method when paramter is limited to AnyRef and AnyVal in Scala
- what is Case.Aux in shapeless
- Method is defined twice
- How could I automatically update and display time(every 10 sec) using Lift Comet
- Parsing a json object which has many fields
- Scala Slick inserting multiple random rows into PostgreSql database
- Multiple Upload Controller in Play Framework / Scala
- Detect environment (prod vs test) with a Lift app deployed on Resin?
- How to schedule a task at specific time in Scala?
- Define method declaration which argument is a implementation type
- Get or create child Akka actor and ensure liveness
- Stream-Static Join: How to refresh (unpersist/persist) static Dataframe periodically
- How to Random Choice element from Enum Scala3
- How is lazy interpreted in recursive context?
- Enriching SparkContext without incurring in serialization issues
- how to add custom metrics to spark mlib evaluator?
- How to wrap multiple sql functions into a UDF in Spark?
- Use map and zip to be more func style in 2 for loops
- Scala Implicits compilation error
- Make spark-sql UDF available in Scala spark data frame DSL API
- Why eclipse complaining about the missing dependency I added?
- Map whole list in Scala
- udf spark column names
- Why does scala hang evaluating a by-name parameter in a Future?
- Scala: modify a NodeSeq
- How to load saved KMeans model (in ML Pipeline)?
- How to apply dot-based JSONPath to Play JsValue
- How to check if element is null or empty in a field constructor?
- spark use underscore as parameter to RDD.flatmap or map