Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a side-effect only function to async using futures. Which is the return type?

Tags:

scala

akka

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.

like image 373
GuidoMB Avatar asked Sep 30 '13 14:09

GuidoMB


1 Answers

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.

like image 186
Travis Brown Avatar answered Sep 30 '22 10:09

Travis Brown