Implement async/await using Fiber

Yuxuan Shui via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 20 12:56:13 PDT 2016


On Friday, 20 May 2016 at 06:40:42 UTC, Jacob Carlborg wrote:
> On 2016-05-20 04:14, Yuxuan Shui wrote:
>
>> Hmm... This could work. But I'm not satisfied with this 
>> solution. What
>> if I have multiple yield sites in the fiber, and each have 
>> different
>> return types?
>>
>> Maybe I should use a Variant?
>
> I think you can view "yield" as a form of "return". If you 
> cannot return different types from a function (without it being 
> a template) it would be weird if you could yield different 
> types.

But this is yield as in coroutine, not yield as in generators. 
Those yields doesn't return value to the caller of the fiber.

Let's have an example:

void func() {
    string a = wait_for_user_input();
    int b = wait_for_user_to_click_a_button();
}

Those two wait()s could use yield internally to transfer 
execution back to the caller and wait for input. But it's 
difficult with current dlang fiber because there's no (elegant) 
way to pass data (preferably with different types) while 
transferring executing to/from fibers.


More information about the Digitalmars-d-learn mailing list