I wonder why a Promise of 'Nothing' never completes when I pass "_" as completion result. I ran into this when I wanted to use a Promise to signal that something has finished:
val promiseWillFinish = Promise()
promiseWillFinish.success(_)
// will time out
Await.ready(promiseWillFinish, 5 seconds)
// will return false
println(promiseWillFinish.isCompleted)
For now I'm using a Promise of Unit, which works fine and is also a little more clear. But I still wonder which the code above ends in a timeout / uncompleted Promise.
I ran this with Akka 2.0 final.
There is no possible value of type Nothing
whatsoever (not null
, none at all). A promise of Nothing
cannot be completed, just as a function with result type Nothing
cannot return.
As there is no value of type Nothing
, there is no way of calling success. In fact you are not calling success, you misinterpret what _ does mean here :
when you declare a var
(and only when you declare it), you can set it to its default value with '_'. var v : Int = _
will set v
to 0
, and var v: String = _
will set it to null
. If you try that with Nothing, var v : Nothing = _
, it will crash. Again, there is no value of type Nothing
.
On the other hand, when you write promiseWithFinish.Success(_)
it is a shortcut for
x => promiseWithFinish.Success(x)
You are creating a function value, not using it, you are doing nothing at all.
I don't think promiseWillFinish.success(_)
means what you think it means. It is partial application, which means the result of that expression is a function. You never called the success
method, just created a new anonymous function for the method invocation.
I guess you had a Promise[Nothing]
and were trying to use _
as a default value of type Nothing
. But by definition there are no values of type Nothing
. See http://en.wikipedia.org/wiki/Bottom_type
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