0

I would like to be able to use a case class something like:

case class A[T](item: T)(implicit refine: T => T = (t: T) => t)

A("Hello")
A("World")(_.toLowerCase)

Such that I only have to define the refine function when needed.

But my case class also needs a ClassTag and I don't know any way I can also include it. For example the folllowing won't compile:

case class A[T](item: T)(implicit refine: T => T = (t: T) => t)(implicit ct: ClassTag[T])

Is there a way I can implicitly pass the ClassTag to A as well as the refine function?

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
user79074
  • 4,937
  • 5
  • 29
  • 57
  • 3
    Why do you need `refine` to be _implicit_? Why not just this: `final case class A[T](item: T)(refine: T => T = identity)(implicit ct: ClassTag[T])`? – Luis Miguel Mejía Suárez Sep 30 '20 at 19:37
  • I guess that making a parameter of type `T => T` implicit is pointless because for every type `T` implicit of type `T => T` exists in every scope. You can check this: `def test[T] = implicitly[T => T]` compiles. You can just omit this implicit parameter. And default value will never be used. – Dmytro Mitin Sep 30 '20 at 20:17
  • 1
    Using the implicit syntax allows me to write A("Hello") as opposed to A("Hello)() – user79074 Sep 30 '20 at 20:24
  • You would have the same syntax with default arguments, e.g. `case class A[T](item: T, refine: T => T = (t: T) => t)(implicit ct: ClassTag[T])` – Suma Sep 30 '20 at 22:19
  • You may find this related: https://stackoverflow.com/questions/22552985/scala-passing-one-implicit-parameter-implicitly-and-the-other-explicitly-is-it – Suma Sep 30 '20 at 22:37
  • @Suma It's worth noting that you've changed `equals`, `hashCode` etc. for `A`. – Dmytro Mitin Oct 01 '20 at 20:46

0 Answers0