TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

What Color is Your Function? (2015)

53 点作者 adunk7 个月前

18 条评论

wesselbindt7 个月前
I&#x27;ve always found the criticism leveled by the colored functions blog post a bit contrived. Yes, when you replace the words async&#x2F;await with meaningless concepts I do not care about such as color, it&#x27;s very annoying to have to arbitrarily mark a function as blue or red. But when you&#x27;re honest with yourself and interpret the word &quot;async&quot; as &quot;expensive&quot;, or as &quot;does network calls&quot;, it becomes clear that &quot;async&#x2F;await&quot; makes important features of your function explicit, rather than implicit. Seeing &quot;await&quot; gives me more information about the function, without having to read the body of the function (and the ones it calls, and the ones they call, etc). That&#x27;s a good thing.<p>There are serious drawbacks to async&#x2F;await, and the red&#x2F;blue blog post manages to list none of them.<p>I&#x27;ve wrote a blog in response to OP containing a more detailed version of the above:<p><a href="https:&#x2F;&#x2F;wpbindt.github.io&#x2F;async&#x2F;opinions&#x2F;programming&#x2F;2024&#x2F;01&#x2F;21&#x2F;red-code-blue-code-honesty.html" rel="nofollow">https:&#x2F;&#x2F;wpbindt.github.io&#x2F;async&#x2F;opinions&#x2F;programming&#x2F;2024&#x2F;01...</a>
评论 #41935626 未加载
评论 #41934778 未加载
评论 #41934989 未加载
评论 #41934783 未加载
评论 #41936673 未加载
评论 #41935444 未加载
littlestymaar7 个月前
Ah my pet peeve reaching the front page again…<p>The “coloring problem” has nothing to do with async at all, it&#x27;s just about making <i>effects</i> explicit or implicit in the code.<p>In Go or Rust, returning an error is also a “color” that spreads out to the top of the call stack. Same for checked exception in Java. Unchecked exceptions are like blocking functions, it&#x27;s invisible so you don&#x27;t have to manually annotate your functions but as a result any function can now fail without the programmer knowing it.<p>JavaScript having async&#x2F;await but unchecked exception may sound bit paradoxical in that regard, but every language has a combinations of both explicit and implicit <i>effects</i>: Rust has both async&#x2F;await and explicit errors, but it also has panics (implicit errors) and implicit allocation for instance.<p>I personally live explicit effects, but at the same time when you have too many of them (for example I&#x27;d like to have effects like “pure”, “allocating” and “panics”) they start to cause combinatorial explosion of cases in libraries accepting closures as parameter.<p>Algebraic effects supposedly solve this particular problem but I&#x27;m not sure the additional conceptual learning effort is sustainable for a mainstream language, so in the meantime I think every language must keep its amount of explicit effects under a certain threshold.
评论 #41936158 未加载
评论 #41935375 未加载
评论 #41937806 未加载
mattxxx7 个月前
I&#x27;ve spend a lot of time writing things using async in rust, python, and typescript, and I still find it un-intuitive &#x2F; conceptually incorrect.<p>Once you&#x27;re in an async function the rules then fundamentally change for how everything operates; it&#x27;s almost like programming within a dialect of the same language. In particular, I&#x27;m referring to everything from function calling, managing concurrency, waiting on results, sleeping threads.<p>Comparatively, when you&#x27;re in a go-block in go, you&#x27;re still writing within the same dialect as outside of it.
评论 #41935470 未加载
omgbear7 个月前
I&#x27;ve thought about this a lot in relation to typescript over the years and had various opinions -- For some time I thought it&#x27;d be better if there was an implicit `await` on every line and require `void` or some other keyword to break execution like `go` in Golang.<p>But, eventually I realized the difference in pre-emption between languages -- Go can (now) preempt your code in many places, so locks and thread-safety are very important.<p>The javascript runtime only preempts at certain places, `await` being one. This means I can know no other code can be running without explicit locks around all critical sections.<p>Finally understanding the trade-offs, I no longer am as frustrated when recoloring a bunch of functions. Instead, I can appreciate the areas where I&#x27;m not required to lock certain operations that I would in other languages.
评论 #41934226 未加载
recursivedoubts7 个月前
My web scripting language, <a href="https:&#x2F;&#x2F;hyperscript.org" rel="nofollow">https:&#x2F;&#x2F;hyperscript.org</a>, tries to hide the difference between sync and async by resolving promises in the runtime. My theory is that this is something that web script writers should not be concerned with (this theory makes more sense when you consider hyperscript is a companion to htmx, and favors a particular approach to scripting[1].)<p><pre><code> on click fetch &#x2F;whatever as json put the result&#x27;s data into #some-div wait 2s put &#x27;&#x27; into #some-div </code></pre> You don&#x27;t have to mark the script as sync or async, and the runtime will resolve everything for you, making everything feel like synchronous scripting.<p>This obviously has limitations and foot guns, but it works reasonably well for light scripting. More info here:<p><a href="https:&#x2F;&#x2F;hyperscript.org&#x2F;docs&#x2F;#async" rel="nofollow">https:&#x2F;&#x2F;hyperscript.org&#x2F;docs&#x2F;#async</a><p>And the implementation of it in the runtime here:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;bigskysoftware&#x2F;_hyperscript&#x2F;blob&#x2F;c81b07cec82af15f7088205467ec6b35f52faa74&#x2F;src&#x2F;_hyperscript.js#L1640">https:&#x2F;&#x2F;github.com&#x2F;bigskysoftware&#x2F;_hyperscript&#x2F;blob&#x2F;c81b07ce...</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;bigskysoftware&#x2F;_hyperscript&#x2F;blob&#x2F;c81b07cec82af15f7088205467ec6b35f52faa74&#x2F;src&#x2F;_hyperscript.js#L1705">https:&#x2F;&#x2F;github.com&#x2F;bigskysoftware&#x2F;_hyperscript&#x2F;blob&#x2F;c81b07ce...</a><p>--<p>[1] - <a href="https:&#x2F;&#x2F;htmx.org&#x2F;essays&#x2F;hypermedia-friendly-scripting&#x2F;" rel="nofollow">https:&#x2F;&#x2F;htmx.org&#x2F;essays&#x2F;hypermedia-friendly-scripting&#x2F;</a>
评论 #41935993 未加载
vitiral7 个月前
This is one of the things I love most about Lua. You can coroutine.yield inside of any function, it&#x27;s up the the caller to handle it correctly.<p>This means I can write my tech stack to run in either mode, then swap out async&#x2F;sync functions in the application. This is exactly what I do in <a href="https:&#x2F;&#x2F;Lua.civboot.org#Package_lap" rel="nofollow">https:&#x2F;&#x2F;Lua.civboot.org#Package_lap</a>
fleabitdev7 个月前
I&#x27;ve spent some time thinking about language design recently. Coloured functions have a surprising number of downsides:<p>- They&#x27;re sometimes much too explicit. When writing a complicated generator, I don&#x27;t necessarily want to annotate every call to a sub-generator with `yield*`, especially if I need to drill that annotation through wrapper functions which aren&#x27;t really generators themselves.<p>- Colours show up everywhere. If you have several functions with a `context: GodObject` parameter, then that&#x27;s a function colour. Most third-party code will be unable to forward a `context` argument to a callback, so you&#x27;ll have to manually smuggle it in using closures instead.<p>- Different &quot;colour channels&quot; don&#x27;t compose nicely with one another. Even though JavaScript ES2017 provided both async functions and generators, ES2018 had to add multiple new pieces of syntax to permit async generators.<p>- It&#x27;s normally impossible to write code which is generic over a function&#x27;s colours. For example, if you have `function callTwice(f)` in JavaScript, you&#x27;d need a separate `async function callTwiceAsync(f)`, and a `function* iterateTwice(iterable)`, and an `async function* iterateTwiceAsync(iterable)`, despite the fact that all of those functions are doing the same thing.<p>Several small languages are experimenting with algebraic effect systems [1], which would make all functions colourless. If JavaScript had this feature, the syntax for defining and calling functions would be the same, no matter whether you&#x27;re dealing with a normal function, a generator, or an async function. No more `async`, `await`, `function*`, `yield`, `yield*`, `for-of`, `for await`...<p>This can make everything too implicit and vague. The state of the art is for algebraic effect systems to be implemented in a statically-typed language, which then uses type inference to quietly tag all function types with their colours. This means that the compiler can enforce requirements like &quot;you can only call generators within a scope which is prepared to collect their results&quot;, but that rule wouldn&#x27;t prevent you from wrapping a generator in `callTwice`.<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;ocaml-multicore&#x2F;ocaml-effects-tutorial">https:&#x2F;&#x2F;github.com&#x2F;ocaml-multicore&#x2F;ocaml-effects-tutorial</a>
评论 #41935214 未加载
throwaway3133737 个月前
A slightly separate but still related question that bothers me every time I, a Python programmer, read about function coloring and different approaches to concurrency:<p>Does anyone understand why asyncio-style approach to async won over gevent-style in Python? Why was the former approach accepted into to stdlib, got special syntax and is preferred by the community while the latter is a fringe niche thing?
评论 #41937363 未加载
评论 #41937340 未加载
bazoom427 个月前
It is similar to IO in Haskell which also force you to explicitly declare it in the signature (and in turn force callers to be IO), where most other languages (even functional) allow you to do IO anywhere.
immibis7 个月前
There is also coloured data: stack vs heap, threadsafe vs not, disk vs memory vs SQL, mutable vs immutable, etc ...<p>Another comment: Python&#x27;s async&#x2F;await is <i>precisely</i> syntactic sugar for generators. It just changes the keywords and checks that you only use await in a function labeled async.<p>Edit: my rate limit appears to have been increased.
jpc07 个月前
I think the main issue with async&#x2F;await specifically is that it is abstracted away from most developers.<p>I may have a sync program running a main loop and I want to have a subset of that program using something like the reactor pattern. In any multithreading aware programming language I can do that by just sticking that in another thread and syncing however I want, even using coroutines I can do that, it doesn&#x27;t need to follow the reactor pattern.<p>However libraries (tokio) and languages (js) don&#x27;t make that the obvious default. In something like JS it isn&#x27;t even possible, the entire language is built on top of coroutines. For tokio the default is &quot;put tokio main here and voila async&quot; and you need to actually understand what it&#x27;s doing under the hood to know that that isn&#x27;t the behaviour you want in all cases.<p>This is more bottom-up vs top-down learning. Most people learn and teach top-down because it gets you to productive really quickly. Bottom-up is much harder and has the chance of getting lost in the weeds but makes what is happening very obvious.<p>Does tokio use reactor or coroutines? It depends...
withinboredom7 个月前
This is how we got &quot;fibers&quot; in PHP and they are absolutely worthless. With async&#x2F;await, I can choose or choose not to wait for the result. Or I can trigger the work asynchronously and wait on it later, in an entirely different function. With Fibers, you cannot choose. You must wait, and you must wait now.
ks20487 个月前
Do any relatively-popular languages avoid “two colors” by essentially making everything async?
评论 #41935787 未加载
pornel7 个月前
This article gets referenced a lot, but it does a poor job of defining what <i>color</i> actually is.<p>The biggest issue the article describes — inability for sync code to wait for an async result — is limited mostly to JavaScript, and doesn&#x27;t exist in most other languages that have async and a blocking wait (they <i>can</i> call red functions from blue functions).<p>If this architectural hurdle is meant to be <i>the color</i>, then lots of languages with async functions don&#x27;t have it. This reduces the coloring problem down to a minor issue of whether async needs to be used with a special syntax or not, and that&#x27;s not such a big deal, and some may prefer async to be explicit anyway.
评论 #41935631 未加载
dang7 个月前
Related. Others?<p><i>Ruby methods are colorless</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=41001951">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=41001951</a> - July 2024 (235 comments)<p><i>On &#x27;function coloring&#x27; (2018)</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=36199499">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=36199499</a> - June 2023 (21 comments)<p><i>I Believe Zig Has Function Colors</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30965805">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30965805</a> - April 2022 (158 comments)<p><i>In Defense of Async: Function Colors Are Rusty</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=29793428">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=29793428</a> - Jan 2022 (101 comments)<p><i>The Function Colour Myth, Or: async&#x2F;await is not what you think it is</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=28904863">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=28904863</a> - Oct 2021 (7 comments)<p><i>What color is your function? (2015)</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=28657358">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=28657358</a> - Sept 2021 (58 comments)<p><i>What Color Is Your Function? (2015)</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=23218782">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=23218782</a> - May 2020 (85 comments)<p><i>What Color is Your Function? (2015)</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=16732948">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=16732948</a> - April 2018 (45 comments)<p><i>The Function Colour Myth (or async&#x2F;await is not what you think it is)</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=12300441">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=12300441</a> - Aug 2016 (4 comments)<p><i>What Color Is Your Function?</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=8984648">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=8984648</a> - Feb 2015 (146 comments)
from-nibly7 个月前
Wouldn&#x27;t adding a way to block on an async function make javascript colorless?<p>No one would want that though right? Like in what context would you even have a consequenceless block? A CLI maybe?
评论 #41936113 未加载
dmvdoug7 个月前
Maybe Chomsky <i>was</i> on to something…<p><i>Colorless green functions async&#x2F;await furiously.</i>
Rapzid7 个月前
I still haven&#x27;t seen a good &quot;solution&quot; to this &quot;problem&quot; for imperative style programming. Why the quotes? Because the functions have different signatures, thus different functions(colors).<p>And IT MATTERS. That&#x27;s why you have structured concurrency hot on the heels of Loom. And once you are using structured concurrency constructs... Ehh, it&#x27;s not the magic &quot;solution&quot; we were sold anymore is it?
评论 #41934431 未加载
评论 #41934341 未加载