4 ms·
There's been a lot of discussion here of the technical reasons for function coloring, but the semantic reasons for it are far more interesting and important to
by lolinder 5y ago
There's been a lot of discussion here of the technical reasons for function coloring, but the semantic reasons for it are far more interesting and important to me. I suspect that technical limitations could (and in some languages do) have workarounds, but there's real value to the system as it's set up.
When I mark a function as `async`, I'm specifying that this function will be performing some form of asynchronous I/O. Any caller of that function (or its callers' callers) will therefore also be doing I/O. Such functions may take a while to "return" and are more likely to throw an exception.
Function coloring makes these properties explicit. By forcing all callers to also be `async`, it becomes easy to see which parts of the codebase are doing (somewhat risky) I/O work and which parts will be near-instant. No one ends up surprised when a call to `getFoo()` ends up getting `foo` from the network rather than the local object.
- gpderetta 5y agoBut async doesn't imply IO in many languages, it could just be a timer or a cross thread operation. At the limit is just lazy evaluation. Similarly the absence of async doesn't guarantee the absence of IO, the function could be doing plain old sync IO or scheduling continuations on the event loop by hand. IMHO there isn't any justification for async in "high overhead" languages, i.e. most of the dynamic languages that are not going to spawn tens if thousands of concurrent coroutines; those languages are better served by green threads or some sort of stackful continuations (the existence of async in python is particularly baffling). It is more of a necessary evil in high performance languages were dedicating a call stack is a comparatively large cost. Even then I strongly believe that async functions should be awaited by default and any forking should be explicit. YMMV.