score:3
Accepted answer
It's a little tricky to get the H
inferred correctly, but you can do it with a <:<
instance:
import shapeless._
case class CellEncoder[A](encode: A => String)
implicit val stringCellEncoder: CellEncoder[String] = CellEncoder(identity)
implicit val intCellEncoder: CellEncoder[Int] = CellEncoder(_.toString)
case class Bar(xs: String)
implicit def caseClass1CellEncoder[A, R, H](implicit
gen: Generic.Aux[A, R],
ev: R <:< (H :: HNil),
c: CellEncoder[H]
): CellEncoder[A] = CellEncoder(
(a: A) => ev(gen.to(a)) match {
case h :: t => c.encode(h)
}
)
(I've made up a simple CellEncoder
for the sake of a complete working example.)
This works because R
can be inferred when the compiler is looking for an Generic.Aux[A, R]
instance, and can then guide the inference of H
when looking for a value for ev
.
Source: stackoverflow.com
Related Query
- Deriving type class instances for case classes with exactly one field
- How to write a symmetric Play Json formatter for a case class with one field in scala?
- Automatic type class derivation for case classes with Scala Enumeration fields
- Play JSON formatter for case class with generic type classes
- Deriving decoder instances of case classes with a single field
- Scala. Can case class with one field be a value class?
- Writing type class instances for nested classes in Scala
- Deriving circe Codec for a sealed case class family where base trait has a (sealed) type member
- Convert one case class to another with the same structure but with one additional field
- scala circe encoders/decoders for an abstract class with case classes
- How to create Dataset with case class Type Parameter ? (Unable to find encoder for type T)
- Build, using Shapeless, generic default instances for case classes with parameters defining a common createValue method
- custom spray-json marshaller for a case class with Value type
- Is there a way to guarantee case class copy methods exist with type classes in Scala?
- Scala 3: Deriving a typeclass for conversion between a case class and a tuple type
- equals of case class with non-primitives is not working correctly for two logically equal instances
- Define common copy for a class hierarchy with many case classes
- How to filter for a class field of a custom entity type with Neo4J OGM?
- How do I define Json format for a case class with more than one apply method?
- How to clone a case class instance and change just one field in Scala?
- Scala: Ignore case class field for equals/hascode?
- How to turn json to case class when case class has only one field
- How do I declare a constructor for an 'object' class type in Scala? I.e., a one time operation for the singleton
- Read CSV in Scala into case class instances with error handling
- Json Serialization for Trait with Multiple Case Classes (Sum Types) in Scala's Play
- Creating instances of a covariant type class from instances of a non-covariant one
- Round-up of Scalaz type class instances for other libraries
- Lift-json extract json with 'type' field into a case class
- Use case and examples for type pattern with type variable
- Best way to use type classes with list parametrized with some base class, abstract class or trait
More Query from same tag
- scala.Null clarification
- Discard wrapper data when using Reads[T]
- Why not make every Scala class a case class?
- How to implement enums in scala slick 3?
- Adding HTML tags to Scala Play output
- scala "error: io error while decoding" "with utf-8"
- Mongoldb-Casbah: get first match in a collection
- hBaseRDD.collect() giving an error
- Scala Type Erasure on Option Match
- Extract list of string into multiple list
- Eclipse: "Output exceeds cutoff limit" in scala worksheet
- Take difference of elements in a list of pairs in Scala
- How to "update" an immutable element in an immutable set in Scala?
- Scala code will not pick up implicit class instance
- Is there a GUI for creating GUIs in Scala?
- Cannot deploy compiled scala play application with sub projects to Heroku
- How to update the duplicates in array in scala
- How to build maven spark project (Scala), then import and config eclipse
- How to make this recursive method tail recursive in Scala?
- Rolling up multiple rows into a single row and column in spark
- declare variable scala as set of instructions
- Functional Programming exercise with Scala
- How to use `flatMap` and `map` to fill a `list` on Play framework + Scala?
- Spark GraphX : Filtering by passing a vertex value in triplet
- Apply on companion object to aliased type
- What does the `#` operator mean in Scala?
- Mapping over generic tuples with polymorphic functions
- Bidirectional operators
- Scala return type for tuple-functions
- Get Scala type name for common AnyVal types/primitives