Idiomatic async programming like C# async/await

Cliff via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 11 20:59:57 PDT 2014


(New to D, old hand at software engineering...)

I come from .NET and have made heavy use of the async/await 
programming paradigm there.  In particular, the Task mechanism 
(futures/promises) lets one encapsulate the future result of some 
work and pass that around.  D seems to have something similar in 
std.parallelism.Task, but this seems to additionally encapsulate 
and expose the actual work to do.

What I want to do is be able to define an interface that performs 
certain possibly-slow operations and presents a Task-based 
interface.  Example in C#:

interface MyStore
{
     Task<Key> Store(byte[] content);
     Task<byte[]> Retrieve(Key key);
}

What I feel like I *want* to do in D is something roughly similar:

interface MyDStore
{
     Task!Key Store(InputRange!ubyte content);
     Task!(InputRange!ubyte) Retrieve(Key key);
}

...but std.parallelism.Task requires parameterization on the 
function which the task would execute - that is clearly an 
implementation detail of the store.

What is the correct D idiom to use in this case?



More information about the Digitalmars-d-learn mailing list