I'm reading the book "Scala with cats". The author says that Semigroupal doesn't always provide the behaviour we expect. And he shows this example:
import cats.Semigroupal
import cats.instances.future._ // for Semigroupal
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.language.higherKinds
val futurePair = Semigroupal[Future].
product(Future("Hello"), Future(123))
Await.result(futurePair, 1.second)
// res1: (String, Int) = (Hello,123)
In the book result is (Hello,123)
but we expected Future(Hello, 123)
. As I understood, we lost the Future
, so there is no expected context.
I decided to reproduce this example and got this result:
Future(Success((Hello,123)))
Context in the place. Hmm. Then I tried this experiment:
val futurePair = Semigroupal[Future]
.product(Future{Thread.sleep(100);"Hello"}, Future(123))
println(futurePair)
The result is Future(<not completed>)
. As I expected.
So I can't understand what's wrong with Future
. I got expected behavior and I didn't lose the context of calculation. Maybe because of Future
start calculation at the moment of creating? But why is it a problem?