Intended use for akka.Done and akka.NotUsed

533 views
Skip to first unread message

rklaehn

unread,
Feb 24, 2016, 5:41:52 AM2/24/16
to Akka User List
Hi all,

given the prominent position of akka.Done and akka.NotUsed: are these only meant to be used within akka streams, or also elsewhere?

I am currently trying to introduce futures into a codebase with a lot of blocking, and it sure would be helpful to distinguish between methods returning Unit where successful return signals successful completion, and methods where successful return means nothing (e.g. because it is sending to some actor inside the method using !).

Would this be a valid use case for using Done and NotUsed?

Cheers,

Rüdiger

Konrad Malawski

unread,
Feb 24, 2016, 5:43:33 AM2/24/16
to akka...@googlegroups.com, rklaehn
Hey Rüdiger,
yeah those can definitely be used in all kinds of APIs.
The reason we have them mostly in Streams for now is because we could not break compatibility in akka-actor for example.

The types are intended to be simply more informative than plain `Unit`, so wherever that's needed feel free to use them :-)

-- 
Cheers,
Konrad 'ktoso’ Malawski
Akka @ Lightbend
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

rklaehn

unread,
Feb 24, 2016, 7:23:25 AM2/24/16
to Akka User List, rkl...@gmail.com
Hi Konrad,

So, if it wasn't for backward compatibility, the return type of tell would be akka.NotUsed, to signal that successful return does not tell you anything about successful processing of the message (akka.Done would certainly be wrong)?

Cheers,

Rüdiger

Patrik Nordwall

unread,
Feb 24, 2016, 7:35:06 AM2/24/16
to akka...@googlegroups.com, rkl...@gmail.com
On Wed, Feb 24, 2016 at 1:23 PM, rklaehn <rkl...@gmail.com> wrote:
Hi Konrad,

So, if it wasn't for backward compatibility, the return type of tell would be akka.NotUsed, to signal that successful return does not tell you anything about successful processing of the message (akka.Done would certainly be wrong)?

That would be to take it one step to far, I would say. Ordinary methods should still be declared with void (java) or Unit (scala) return types. Done and NotUsed are mostly interesting when the return value is not immediate, such as in a CompletionStage (java), Future (scala), or stream materialized value.



--

Patrik Nordwall
Lightbend -  Reactive apps on the JVM
Twitter: @patriknw

 Lightbend

Konrad Malawski

unread,
Feb 24, 2016, 9:37:17 AM2/24/16
to akka...@googlegroups.com, Patrik Nordwall, rkl...@gmail.com
Yeah, agree with Patrik here.
It's mostly for `Future[Done]` and situations like that, void/Unit is still fine in normal return values.

-- 
Cheers,
Konrad 'ktoso’ Malawski
Akka @ Lightbend

Roland Kuhn

unread,
Feb 24, 2016, 11:12:24 AM2/24/16
to akka-user, Patrik Nordwall, rkl...@gmail.com
There is one other use that these types can be put to: Scala’s habit of conjuring a Unit out of thin air where needed can make it non-obvious where all the exit points of a method are. Declaring a different return type means that all such points need to be marked out explicitly. But outside the method this is not very useful (and this was also not the reason for the introduction of these types). As Patrik and Konrad note these are intended to be used a type parameters.

Regards,

Roland


Dr. Roland Kuhn
Akka Tech Lead
Typesafe – Reactive apps on the JVM.
twitter: @rolandkuhn


Bruno Bieth

unread,
Apr 27, 2016, 10:41:07 AM4/27/16
to Akka User List, patrik....@gmail.com, rkl...@gmail.com
I agree with Roland that having a Future[Unit] may lead to subtle bugs due to the value discarding mechanism. My favorite is mixing map and flatMap by mistake:

// this compiles just fine: map[T] is infered to be map[Unit], then Future { println("two"); Done } which is of type Future[Done] is discarded to satisfy map[Unit]
def compute: Future[Unit] = Future[Done] { println("one"); Done }.map( _ => Future { println("two"); Done } )

On the other hand:

// won't compile!
def compute: Future[Done] = Future[Done] { println("one"); Done }.map( _ => Future { println("two"); Done } )

Now, this could be a good reason for bringing this into the scala library?

Bruno

Bruno Bieth

unread,
Apr 28, 2016, 3:13:38 AM4/28/16
to Akka User List, patrik....@gmail.com, rkl...@gmail.com
Reply all
Reply to author
Forward
0 new messages