Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promise of 'Nothing' is never completed?

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.

like image 908
Gamlor Avatar asked Dec 04 '22 17:12

Gamlor


2 Answers

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.

like image 195
Didier Dupont Avatar answered Feb 01 '23 08:02

Didier Dupont


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

like image 24
Ben James Avatar answered Feb 01 '23 09:02

Ben James