I have the method below:
override def insertAll(notifications: Seq[PushNotificationEncoded])
(implicit ec: ExecutionContext): Future[Unit] = {
val f = Future.sequence(notifications.map(insert)).map(_ => Unit)
f.onFailure { case ex => sys.error(s"Got exception whilst saving PushNotification: $ex") }
f
}
It gives the following compiler error:
type mismatch;
found : scala.concurrent.Future[Unit.type]
required: scala.concurrent.Future[Unit]
f
^
I thought Unit was a type with a single element, thus there shouldn't be any confusion when it comes to Unit. I tried googling this but I couldn't find any more information about what "Unit.type" is.
If I simplify the method like this, then it works fine:
override def insertAll(notifications: Seq[PushNotificationEncoded])
(implicit ec: ExecutionContext): Future[Unit] =
Future.sequence(notifications.map(insert)).map(_ => Unit)
What is going on here?
Unit's sole instance is ()
. Unit
used as a value is the companion object for Unit (which mainly manages boxing and unboxing)
val f = Future.sequence(notifications.map(insert)).map(_ => ())
x.type
is the singleton type of x
: the type whose only two values are x
and null
. x
must be a "stable identifier" of a value, such as the companion object of Unit
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With