Given a signature like this one or that one:
def foo[A, F[_]](implicit mon: Monoid[F[A]], pr: Pure[F]): F[A]
Assuming A is Char, is there a way to get a String instead of a List[Char]?
String does not take a type parameter, so I assume it’s not possible. What’s the next best option? Right now, I use mkString on the result, but that doesn’t feel optimal.
I think String is a monoid with zero "" and append +…
It is possible to persuade String to masquerade as a higher-kinded type, and hence allow functions of the form of
footo be applicable. However, Scala’s type inference isn’t currently up to the job of inferringfoo‘s type arguments, so you’ll have to supply them explicitly,Note that the
Chartype argument tofoois unused and can be anything, but must be something: in this case eitherCharis the natural choice, butNothingorAnywould do as well.Note that this solution trades on
String‘s special characteristics: values of all types are convertible toStrings sopure[A](a : => A) : Stringis implementable for all typesA. Replicating this idiom for types other thanStringwould most likely have to exploit some mechanism for implementing type-specific cases in the body ofpure(eg. a pattern match of some sort).