When I have a generic class like this
case class C [E] (errors : Seq[E]){
def merge [E1 <: EX, EX >: E] (errors1 : Seq[E1]) = Seq[EX]() ++ errors ++ errors1
}
everything works - it merges sequences of errors into a sequence of common supertype. However when I mix this with the type class pattern it does not infer the common supertype correctly.
I have implemented HList application according to this approach and it works by itself. Now I want to use it for building argument list where each argument may fail. Finally I want to be able to apply this argument list to a function which may fail itself. So the output errors sequence type should be supertype of the list errors and function errors. (The next step would be to lift the restriction that the function must return ErrorProne and flatten it via some typeclass but I consider this a more simple case.) The failing argument list wrapper goes like this. Notice that the :: method works.
case class SuccessArgList [E, L <: HList[L]] (list : L) extends ArgList[E, L] {
override def apply[S, E1 <: EX, EX >: E, F](fun : F)(implicit app : HApply[L, F, ErrorProne[E1, S]])
: ErrorProne[EX, S] = app.apply(list, fun)
override def :: [A, E1 <: EX, EX >: E] (argument : ErrorProne[E1, A]) : ArgList[EX, A :: L] = argument match {
case Success(a) => SuccessArgList(a :: list)
case Failure(e) => FailureArgList(e)
}
}
where E is type of error of current failing argument list, L is the the type of actual argument list, S is the function success value type, E1 is the function failure value type, EX should be common supertype of the errors and F is the function. The fail list goes similarily and the ErrorProne container is like this (I could probably use Either but that should not be relevant)
sealed trait ErrorProne[+F, +S]{
def f[To] (implicit flatrener : FlattenErrorProne[ErrorProne[F, S], To]) = flatrener.flatten(this)
}
case class Success [+F, +S] (result : S) extends ErrorProne[F, S]
case class Failure [+F, +S] (errors : Seq[F]) extends ErrorProne[F, S]
When trying to use the apply method of the argument list type arguments are inferred wrong determining the E1 to E, therefore the application is not found. Can the inference be fixed? Are there materials on rules by which the inference goes? I cannot find any specification.