I have a function with the following signature
def reject[A](errors: List[String]): ValidationNEL[String, A]
Since this is a reject method, the type A would never be returned but I need it to be reflected in order to match the signature. I was messing around with type lambdas to get my desired result as follows:
errors.map(Failure[String, A](_).liftFailNel).sequence[({type l[a] = ValidationNEL[String, a]})#l, A]
This uses the type List[A] (or appears to), rather than my desired type A. Is there a standard way to derive the result I am looking for?
Because
errorsmay be an emptyList, and you are constraining yourself to have no value of typeA, I don’t think you can write this as a total function. To write this type signature, you would need to cheat by pretending the empty list case does not exist, e.g.Edit: As Apocalisp pointed out, you can of course make this a total function, by introducing an error for the empty list. But I would only do that if
errorsis computed at runtime, and I suspect this is not your use case, as it would lead to silly errors such as:Why not pass the
errorsas aNonEmptyListinstead – presumably you only use this function if you have errors to use at compile time.You can make this terser to use by copying the signature of
NonEmptyList.apply(and specialising it toString):Let’s try it out: