Future.onSuccess

224 views
Skip to first unread message

Nils Kilden-Pedersen

unread,
May 7, 2013, 2:53:56 PM5/7/13
to scala-user
I guess I know why this doesn't work, but I'm not sure why it's not allowed, i.e. why a partial function? Seems syntactically heavier and I could just match if that was needed.

scala> def slow = {Thread.sleep(2000); "Slow string"}
slow: java.lang.String

scala> Future(slow).onSuccess(println)
<console>:18: error: type mismatch;
 found   : Unit
 required: PartialFunction[java.lang.String,?]
              Future(slow).onSuccess(println)
                                     ^

Rex Kerr

unread,
May 7, 2013, 3:26:08 PM5/7/13
to scala-user
You have foreach and map available to handle the full function cases.  onSuccess and onFailure are specifically for partial functions.

  --Rex



--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Nils Kilden-Pedersen

unread,
May 7, 2013, 3:27:36 PM5/7/13
to Rex Kerr, scala-user
Indeed I do. Although somewhat unexpected to have foreach and map be blocking calls.

Thanks.

Rex Kerr

unread,
May 7, 2013, 3:30:46 PM5/7/13
to Nils Kilden-Pedersen, scala-user
You mean for them to _not_ be blocking:

scala> Future{ Thread.sleep(1000); println("Hi"); "Bye" }.foreach(println); println("Look!")
Look!

scala> Hi
Bye

   --Rex

Nils Kilden-Pedersen

unread,
May 7, 2013, 3:38:10 PM5/7/13
to Rex Kerr, Nils Kilden-Pedersen, scala-user
Ha, well no. :-)

What I meant was that it's unexpected to have them be asynchronous.

Matthew Pocock

unread,
May 7, 2013, 3:52:24 PM5/7/13
to Nils Kilden-Pedersen, Rex Kerr, scala-user
The key thing, I think, is the sequencing, not which thread things run on. As long as the things that are requested to run before or after each other do run in that order, all is good. Beyond that, you should plan for the maximal asynchronousity but perhaps expect not so much ;)

Matthew
Dr Matthew Pocock
Turing ate my hamster LTD

Integrative Bioinformatics Group, School of Computing Science, Newcastle University

skype: matthew.pocock
tel: (0191) 2566550

Nils Kilden-Pedersen

unread,
May 7, 2013, 4:30:29 PM5/7/13
to Matthew Pocock, Nils Kilden-Pedersen, Rex Kerr, scala-user
The question then becomes, is there any way to block on a Future?

Nils Kilden-Pedersen

unread,
May 7, 2013, 4:36:21 PM5/7/13
to Matthew Pocock, Rex Kerr, scala-user
Presumably using `ready` or `result`. Although the syntax is a bit unusual (seen through Java Future glasses). An implicit CanAwait is expected. And then the docs say that I shouldn't really use those methods, but instead use Await.ready/result. Which then begs the question of why they're public then. Oh well....

√iktor Ҡlang

unread,
May 7, 2013, 4:37:04 PM5/7/13
to Nils Kilden-Pedersen, Matthew Pocock, Rex Kerr, scala-user

abstract defresult(atMost: Duration)(implicit permit: CanAwait)T

Await and return the result (of type T) of this Awaitable.

This method should not be called directly; use Await.result instead.

atMost

maximum wait time, which may be negative (no waiting is done), Duration.Inf for unbounded waiting, or a finite positive duration

returns

the result value if the Awaitable is completed within the specific maximum wait time

Definition Classes
Awaitable
Annotations
@throws( cause = classOf[Exception] )
Exceptions thrown
IllegalArgumentException

if atMost is Duration.Undefined

InterruptedException

if the current thread is interrupted while waiting

TimeoutException

if after waiting for the specified time this Awaitable is still not ready


Viktor Klang
Director of Engineering

Twitter: @viktorklang

Luis Ángel Vicente Sánchez

unread,
May 7, 2013, 4:40:51 PM5/7/13
to Nils Kilden-Pedersen, scala-user, Matthew Pocock, Rex Kerr

You could block using scala.concurrente.Await.

Nils Kilden-Pedersen

unread,
May 7, 2013, 4:41:00 PM5/7/13
to √iktor Ҡlang, Nils Kilden-Pedersen, Matthew Pocock, Rex Kerr, scala-user
Yes, those docs. Were you trying to imply something?

Nils Kilden-Pedersen

unread,
May 7, 2013, 4:42:15 PM5/7/13
to Nils Kilden-Pedersen, √iktor Ҡlang, Matthew Pocock, Rex Kerr, scala-user
I guess a race condition :-)

But why are those methods public if they shouldn't be called directly?

√iktor Ҡlang

unread,
May 7, 2013, 4:43:46 PM5/7/13
to Nils Kilden-Pedersen, Matthew Pocock, Rex Kerr, scala-user
I answered your question?

Nils Kilden-Pedersen

unread,
May 7, 2013, 4:45:24 PM5/7/13
to √iktor Ҡlang, Nils Kilden-Pedersen, Matthew Pocock, Rex Kerr, scala-user
Yes, but it wasn't clear which of the posts you were replying to. I assumed a later post than I guess it was.

√iktor Ҡlang

unread,
May 7, 2013, 4:47:18 PM5/7/13
to Nils Kilden-Pedersen, Matthew Pocock, Rex Kerr, scala-user
JVM interfaces only have public abstract methods (see Awaitable). (and consider Java usage)

We wanted people to have to go through Await for all blocking, to make it easier to do the right thing (async).

Cheers,

√iktor Ҡlang

unread,
May 7, 2013, 4:47:57 PM5/7/13
to Nils Kilden-Pedersen, Matthew Pocock, Rex Kerr, scala-user
Your email, which I replied to, was quoted below my reply.

(as I do always)

Cheers,

Nils Kilden-Pedersen

unread,
May 7, 2013, 4:49:31 PM5/7/13
to √iktor Ҡlang, Nils Kilden-Pedersen, Matthew Pocock, Rex Kerr, scala-user
Ah, it was hidden by gmail and I clicked the other hidden part. Sorry about that.

Som Snytt

unread,
May 7, 2013, 5:36:56 PM5/7/13
to √iktor Ҡlang, Nils Kilden-Pedersen, Matthew Pocock, Rex Kerr, scala-user
On Tue, May 7, 2013 at 1:43 PM, √iktor Ҡlang <viktor...@gmail.com> wrote:
I answered your question?


...with a question?

But seriously, I'd endorse Nils' original point, that it's easy for us, pardon the expression, dumb people to forget when a PF is expected.

There have been recent threads about:

1) if the expected type is TupleN => R, let me write:

c map ((x,y,z) => r)      // or similarly for Foo(x) => res

to mean

c map (_ match { case (x, y, z) => r })

2) if the expected type is A => Boolean, let me write:

c filter p

to mean

c filter { case p => true; case _ => false }

Actually, the SIP is to allow c filter (_ match p).  So I munged that.

Maybe there's a suitably wishy-washy Episcopalian middle ground whereby one could forget whether a =?> is expected and just use a pattern, and scalac will desugar it correctly.

Future(expr) onSuccess (MyResult(x,y) => println(x * y))

to mean

f onSuccess { case MyResult(x,y) => println(x * y) }

Meanwhile, I've kind of forgotten if it was desugared to a partial or complete function.

Do I care?  We, pardon the expression, dumb people, don't really.  Though after we've finished working the crossword puzzle in pencil, we might enjoy figuring that out.

To return to the original example,

f onSuccess println

could maybe possibly fall back to

f onSuccess ((x => Option(g(x))).unlift)  // or whatever

I don't know that I typed enough parens there.

> I know why this doesn't work, but I'm not sure why it's not allowed.

I hope this slogan makes it onto a Scala fleece hoodie, medium, in "heather".

√iktor Ҡlang

unread,
May 7, 2013, 6:28:37 PM5/7/13
to Som Snytt, Nils Kilden-Pedersen, Matthew Pocock, Rex Kerr, scala-user
I guess this is the old "why isn't Function1 a PartialFunction" discussion?

val pf: PartialFunction[Any, Unit] = { case _ => () } // Total?
val f: Function[Any, Unit] = _ => () // Total?

To which degree is one more "partial" than the other?

Cheers,

Som Snytt

unread,
May 7, 2013, 6:48:33 PM5/7/13
to √iktor Ҡlang, Nils Kilden-Pedersen, Matthew Pocock, Rex Kerr, scala-user
Sorry, I was about to write, and somehow forgot through distraction:

...Apologies In Advance, I'm sure the smart people have hashed this over already.

But this lapse gives me the opportunity to propose another t-shirt:

"I'm partial to functions."
Reply all
Reply to author
Forward
0 new messages