Re: [Vala] [Async] why use .begin ?

On Fri, Sep 18, 2009 at 18:34:47 +0200, Michael 'Mickey' Lauer wrote:
On Friday 18 September 2009 18:18:33 JM wrote:
Hi all
A few days ago I edited the asynchronous stream reading example on the
vala site to use the new async syntax.

After the release of vala-0.7.6 I realized that other examples of the
new syntax use '.begin()' to call the async function.
I compiled the example with and without the .begin keyword and found NO
DIFFERENCE in the generated C code. Both ways are working properly.

Is there a case where the difference will be more clear?

Right now I think they're equal, although my view is that it should make a 
difference when calling an async function from a synchronous one. The principle 
of least surprise should be that begin() and end() would be collapsed to one 
synchronous call in that particular case.

No, the call without .begin() is being deprecated.

From yestarday's discussion on IRC I understood it's not generally possible
to collapse to synchronous call automagically. Doing it involves running
a (recursive) main loop until the callback is called, but this brings a lot
of problems like something quitting the outer loop, the nested loops being
quit out of order and such. Therefore it should not be done without user's
explicit request.

The reason for .begin() is mostly a symetry with .end() and code readability
where the use of .begin() clearly indicates that asynchronous operation is
being started (and will continue beyond the statement).

Right now async has a related problem though for async methods that return 
values, i.e. the following code:

async int func()
        return 10;

void main()
        int i = func();

leads to the -- somewhat surprising -- error message:

mickey andromeda:/local/pkg/vala/test$ valac --pkg gio-2.0 foo.vala
foo.vala:8.10-8.15: error: invocation of void method not allowed as expression
      int i = func();

since internally the invocation is moved to func.begin(), which is (always) a 
void method. This is no problem from async to async as we would be using int i 
= yield func() in that case, however it's quite common to call async() from 
sync(), hence some additional work is required here.

There are two ways to call asynchronous method from synchronous one. You may
want to just start the operation and register a callback for it or you may
want to wait in a new instance of main loop until the operation completes.
The bare func() would be somewhat ambiguous in this respect and so it's being

There may eventually be some construct to call asynchronous operation
synchronously, but it is unlikely to be a simple call.

                                                 Jan 'Bulb' Hudec <bulb ucw cz>

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]