I would like to achieve the following functionality:
case class ValidatorClean[+A](apply: A => A)
implicit val traversableValidatorClean = ValidatorClean[Traversable[String]](_.map(_.trim))
so that traversableValidatorClean
gets picked whenever a ValidatorClean[Seq[String]]
or ValidatorClean[List[String]]
is needed, for example.
However, this doesn't compile, with the error
covariant type A occurs in contravariant position in type => A => A of value apply
I understand that a function is contravariant in its input and covariant in its output, but I want A
to behave invariantly in apply
. That is, the function in apply
will always return the exact same type as its input.
Can this be achieved?