2

I'm trying to understand the Scala type system, and generalize the concept of Future.sequence which has type List[Future[T]] => Future[List[T]]. We can try to generalize this concept by defining a function of type A[B[C]] => B[A[C].

What is the best way to do this? I was thinking of something along the lines of the following (note that I'm using names that suggest this is a monoid, but it's not really a monoid even though it looks kind of similar):

trait Flippable[A[B[C]], B[_], C] {
  def mappend(a1: B[A[C]], a2: B[C]): B[A[C]]
  def mzero: B[A[C]]
}

The above snippet does not compile because the types not consistently constrained to work with the api of the mappend and mzero. How can I specify appropriate constraints to make this compilable so that I can correctly generally define a function like Future.sequence? Are there existing higher order constructs/concepts that can be used (similar to monoid) to avoid defining the flippable trait from scratch as I'm attempting to do above?

jonderry
  • 23,013
  • 32
  • 104
  • 171

0 Answers0