A common gotcha when working with futures is that when you expect Future[Unit], even Future[Future[Unit]] will be accepted (see e.g. Why Shouldn’t You Use Future[Unit] as a Return Type in a Scala Program).
I was surprised recently Future.sequence(setOfFutures) is not accepted in such situation:
import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global
val set = Set(Future(()))
def fuu: Future[Unit] = {
Future.sequence(set)
}
With Scala 2.12.13 I get error:
type mismatch;
found : scala.concurrent.Future[scala.collection.immutable.Set[Unit]]
With Scala 2.13 I get:
Cannot construct a collection of type Unit with elements of type Unit based on a collection of type scala.collection.immutable.Set[scala.concurrent.Future[Unit]].
When I change the body of the function to:
val s = Future.sequence(set)
s
I get set the same error as before.
Why is Future[Future[Unit]] accepted as a Future[Unit] and Future[Set[Unit]] or Future[List[Unit]] is not?