D community's view on syntactic sugar
Neia Neutuladh
neia at ikeran.org
Tue Jun 19 20:06:38 UTC 2018
On Monday, 18 June 2018 at 20:54:22 UTC, aberba wrote:
> async/await make asynchronous code in C# and JavaScript look
> clean and easy to wrap ones head around it. Solution to aka.
> callback hell. If popularity is what you're looking at, it
> JavaScript not Go. And async/await is all over the place...it
> more that a syntactic sugar, it a pattern.
Threads make it easy to write code that does long-running things
like IO without blocking your process. Continuation-passing style
is an answer to your platform not having threads. Async/await is
an answer to the problems of heavily nested scopes and awkward
error handling in the continuation-passing style.
D has threads.
The one uncommon case that suffers here is starting operations in
parallel and collecting their results afterwards. I believe Vibe
has an implementation of futures and a convenience function to
run code in a separate thread and get a Future holding the result.
This does hold up one thread waiting for the others to complete,
which is mildly suboptimal, but you can solve that as well as
async/await by arbitrarily picking one long-running operation to
execute in the current thread.
>> ‘async’ is viral keywords that poorly scales, I worked on Dart
>> core team and even they admitted this problem is not solved
>> well.
>>
> Doesn't scales for what?
You generally can't call an async function from a non-async
function, and you need some extra dressing on an async function
call. (In C# you can manually get the Task<> back and use that,
but that's painful.)
So you use all async IO. You write a function. It works in memory
and doesn't do IO, so you make it easy to call: you make it a
sync function.
Then you change it, and now it does IO. (Maybe it checks values
from a config file, and you change it to check if the config file
changed on disk.) Now you have to mark the function async and
change every call site to add the await keyword.
Except some of these functions might not have been async already.
So you have to mark them async and repeat the process across your
whole codebase.
You could make everything async by default, but then calling
functions is harder. Only a little, but calling functions is
probably like 20% of my expressions, and I could do without the
syntactic bitter yuck.
More information about the Digitalmars-d
mailing list