0

How to use sequence function for List[EitherT[Future, String, CustomObj]] with custom class CustomObj ? I want something like that :

import scala.language.postfixOps
import cats.instances.list._
import cats.syntax.traverse._
import cats.data.EitherT
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

case class CustomObj(int: Int)

private val fst: EitherT[Future, String, CustomObj] =
  EitherT.pure[Future, String](CustomObj(1))
private val snd: EitherT[Future, String, CustomObj] =
  EitherT.pure[Future, String](CustomObj(2))

val source: List[EitherT[Future, String, CustomObj]] = fst :: snd :: Nil

val result: EitherT[Future, String, List[CustomObj]] = source.sequence

import scala.concurrent.duration._
val res = scala.concurrent.Await.result(result.value, 1 second)
println(res) // Right(List(CustomObj(1), CustomObj(2)))

Every time during compile I get

 error: Cannot prove that EitherT[Future,String,CustomObj] <:< G[A].

What does it mean <:< G[A] ?

Dex
  • 13
  • 2
  • @erip [A note on sequencing](https://typelevel.org/cats/typeclasses/traverse.html#a-note-on-sequencing) – Yaneeve Nov 20 '18 at 15:22
  • What do you mean it "doesn't work"? – erip Nov 20 '18 at 15:45
  • I get : error: Cannot prove that cats.data.EitherT[Future, SomeType1, SomeType2] <:< G[A]. Actually I can't understand why it happens.. – Dex Nov 20 '18 at 16:33
  • You should create a minimal example that we can copy and paste. I can't debug this without more info. – erip Nov 20 '18 at 17:35
  • I've edited my post – Dex Nov 21 '18 at 09:46
  • 1
    scalacOptions += "-Ypartial-unification" . Just add it to build.sbt or pom.xml if you use maven. This does the trick. Now everything works fine. Thanks for your comments. – Dex Nov 21 '18 at 10:07

1 Answers1

0

This is an edited version, code @ https://scastie.scala-lang.org/Yaneeve/Jro89ZHwS3G23Aveanxc5A:

import scala.language.postfixOps
import cats.implicits._
import cats.data.EitherT
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

case class CustomObj(int: Int)

private val fst: EitherT[Future, String, CustomObj] =
  EitherT.pure[Future, String](CustomObj(1))
private val snd: EitherT[Future, String, CustomObj] =
  EitherT.pure[Future, String](CustomObj(2))

val source: List[EitherT[Future, String, CustomObj]] = fst :: snd :: Nil

val result: EitherT[Future, String, List[CustomObj]] = source.sequence

import scala.concurrent.duration._
val res = scala.concurrent.Await.result(result.value, 1 second)
println(res) // Right(List(CustomObj(1), CustomObj(2)))

Initially I wrote:

Notice please that it didn't compile and produced odd compile messages until I added import scala.concurrent.ExecutionContext.Implicits.global

And that holds true, BUT your trouble did not lie there. When I went to reproduce again, I got the error you had specified. The thing that was missing is an SBT/scalac flag: scalacOptions += "-Ypartial-unification"

Yaneeve
  • 4,751
  • 10
  • 49
  • 87
  • Thanks a lot .. but I get : error: Cannot prove that cats.data.EitherT[Future, SomeType1, SomeType2] <:< G[A]. Actually I can't understand why it happens.. – Dex Nov 20 '18 at 16:31
  • What are `SomeType1` `SomeType2`? They are not in your question – Yaneeve Nov 20 '18 at 16:42
  • it doesn't matter.. let it be : EitherT[Future, String, CustomObj] The error is: Cannot prove that cats.data.EitherT[Future, String, CustomObj] <:< G[A]. – Dex Nov 20 '18 at 16:54
  • https://scastie.scala-lang.org/Yaneeve/Jro89ZHwS3G23Aveanxc5A - I have reworked it here slightly an I will amend my answer accordingly – Yaneeve Nov 20 '18 at 21:45
  • I still get error: Cannot prove that EitherT[Future,String,CustomObj] <:< G[A]. What does it mean <:< G[A]. ? – Dex Nov 21 '18 at 09:47
  • Truly odd, did you add the flag and reload the project? You can see that the scastie compiles and runs – Yaneeve Nov 21 '18 at 09:48
  • https://stackoverflow.com/questions/30606755/how-does-the-operator-work-in-scala AND https://www.scala-lang.org/api/2.12.3/scala/Predef$.html#%3C:%3C[-From,+To]extendsFrom=%3ETowithSerializable – Yaneeve Nov 21 '18 at 09:51
  • In my project I use maven and sbt... I added this flag also to my pom.xml and it did the trick . Thanks a lot. – Dex Nov 21 '18 at 10:04
  • Happy to help :) – Yaneeve Nov 21 '18 at 10:26