3

I have started a project to write an async PostgreSQL driver on Scala and to be async, I need to accept callbacks and use futures, but then accepting a callback and a future makes the code cumbersome because you always have to send a callback even if it is useless.

Here's a test:

"insert a row in the database" in {
 withHandler {
 (handler, future) =>
 future.get(5, TimeUnit.SECONDS)
 handler.sendQuery( this.create ){ query => }.get( 5, TimeUnit.SECONDS )
 handler.sendQuery( this.insert ){ query => }.get( 5, TimeUnit.SECONDS ).rowsAffected === 1
 }
}

Sending the empty callback is horrible but I couldn't find a way to make it optional or anything like that, so right now I don't have a lot of ideas on how this external API should look like.

It could be something like:

handler.sendQuery( this.create ).addListener { query => println(query) }

But then again, I'm not sure how people are organizing API's in this regard. Providing examples in other projects would also be great.

yannis
39.7k40 gold badges185 silver badges218 bronze badges
asked Mar 10, 2012 at 12:26

2 Answers 2

3

If you are talking async and futures in Scala, it is imperative that you read SIP-14: Futures and Promises. There were also some extensive discussions about it on the SIP mailing list, which is a google group, so you can find its archives.

answered Mar 10, 2012 at 21:16
2

No, you just use futures and don't call get on them unless absolutely necessary. Instead, you add callbacks to the futures and transform and compose futures with combinators. See Twitter Util futures and Akka futures.

answered Mar 10, 2012 at 18:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.