I am working for the first time in a real project using Akka and Scala. While designing an interface for a DAO (which will have two implementations: memory and mongo) I came with the following doubt. Suppose we have a sync version of UserRepository like this
trait UserRepository {
def find(id: Int): Option[User]
def save(user: User): User
def delete(user: User): Unit
}
and an async version of it would be something like this:
trait UserRepository {
def find(id: Int): Future[Option[User]]
def save(user: User): Future[User]
def delete(user: User)
}
How would you define the return time of the delete method? Future[Unit]? Future[Any] Future[Void]? What is the best type to return in an async operation which I only care if its side-effects are applied successfully. I don't have nothing to return but I would like to know if the deletion was successfully completed or in case of an exception being able to register an onFailure callback.
You should use Future[Unit]
for exactly the same reason that you use Unit
in the synchronous case—you need to return something, but don't have anything you need to return. Future[Any]
on the other hand means "Here's something, but I won't tell you what it is". And let's not even talk about what Future[Void]
would mean.
If you don't want to take my word for it, do a quick Google search for "Future[Unit]" scala
and you'll find lots of cases of other people using it in precisely this context.
You can also find this idiom in the use of IO ()
in Haskell—where ()
is the unit type and IO
is in some ways similar to Scala's Future
—to represent computations where we only care about the side effects.
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