tiran
tiran

Reputation: 2431

scala compiler complains weirdly when compiling a foldleft with scalaz function

I recently happen to work with scalaz >>=. I put all the methods which should be bind with >>= in a list and foldleft as follows,

val dataMap:Map[K,V]

def call[F](funcList:List[funcOb[K, V, F]]):Either[F,Seq[(K,Option[V])]] = {
  type t[a] = Either[F,a]
  funcList.
    map(v => {
      v.funcs.
        foldLeft((v.name,dataMap.get(v.name)).right[F])( _ >>= _ )
    }
  ).sequence[t,(K,Option[V])]
}

case class funcOb[K,V,F]( name:K,
     funcs:List[(K,Option[V]) => Either[F, (K, Option[V])]] = List.empty )

now I'm getting a funny error complaining that the required is similar to found

...: type mismatch;
[error]  found   : (K, Option[V]) => Either[F,(K, Option[V])]
[error]  required: (K, Option[V]) => Either[F,(K, Option[V])]
[error]             foldLeft((v.name,dataMap.get(v.name)).right[F])( _ >>= _ )

I cannot understand this behavour. Is there anything missing?

Upvotes: 1

Views: 78

Answers (1)

missingfaktor
missingfaktor

Reputation: 92076

It requires a Function1[(A, B), C]. Your list contains functions of type Function2[A, B, C]. So you need to convert the function to tupled form before you can apply it.

The following works:

def call[F](funcList: List[funcOb[K, V, F]]): Either[F, Seq[(K, Option[V])]] = {
  type t[a] = Either[F, a]
  funcList.
    map(v => {
      v.funcs.
        foldLeft((v.name, dataMap.get(v.name)).right[F])(_ >>= _.tupled)
    }
  ).sequence[t, (K, Option[V])]
}

Upvotes: 2

Related Questions