In respective debates online astir function colors over the years, group person based on the apparently reasonable position that usability arguments tin represent colors arsenic well.
For instance, in Go, location is simply a context.Context value, which manages timeouts, cancellation, and a mini magnitude of information that tin beryllium threaded through various functions. It is, by design, intended to beryllium thing passed through functions moreover if the receiver doesn’t usage it straight but only passes it along. Generally, erstwhile 1 usability starts utilizing a context, you should thread it on to each called functions that could conceivably person a usage for it.
So, superficially, this appears to beryllium a color. Once you person this particular statement successful hand, you “have” to telephone each early functions of that type pinch that argument.
Extending this retired you tin extremity up arguing that each usability parameters are “colors”, arsenic immoderate do.
A quick, though still important, objection is to observe that if this was the case, we wouldn’t beryllium talking astir this astatine all. If async was simply 1 “color” successful a dazzling chaotic rainbow of thousands of different colors, this “color” conception wouldn’t person been invented 50+ years aft the invention of programming connection functions. There is still thing chopped astir really async useful successful immoderate languages that is worthy trying to seizure and understand.
But knowing the business profoundly from 1 illustration is difficult. Here is my effort to move it into a criterion alternatively than a azygous example:
Color As A Change Dependency Graph Shape
Let’s building the problem this way:
Suppose I person to alteration an property of immoderate usability 4 levels buried in a telephone stack, beryllium that a parameter that it takes, whether it is “async” aliases not, aliases immoderate different point for illustration “whether aliases not it has ASM in it” aliases immoderate different property of the function. Any alteration to the function at all, depending connected what characteristics the section connection permits functions to have.
This alteration could consequence successful having to alteration the callers arsenic well.
There’s 3 basal cases that tin emerge:
-
No alteration to immoderate caller is necessary.
-
The alteration whitethorn require a alteration to the nonstop caller.
-
The alteration whitethorn require a alteration to each functions successful the stack.
Graphically, we person the following. For the no-color, function parameter case, the “need to change” narration whitethorn look for illustration this:

For the colour case, it looks for illustration this:

The quality betwixt a “color” and a “normal” alteration is that successful the case of the color, the alteration to the bottom usability successful the stack does not conscionable impact its parent. It immediately and inescapably affects each usability supra it successful the call stack, and therefore, they each must respond to the change.
For a “normal” change, it only propagates to the caller. It may then concatenation to its caller. If it propagates each the measurement up to the top-level function, arsenic demonstrated successful the diagram, this may seem for illustration a favoritism without a difference.
But the cardinal is that the sketch isn’t showing the communal case, it is showing the worst case. In practice, functions almost always break the concatenation earlier it reaches to the apical of the telephone stack. Any function tin “catch” the propagating alteration and encapsulate it so that functions supra it won’t spot it anymore.
You tin measurement this by really often you do successful truth extremity up threading a change up the stack… well… you could backmost successful the Bad Old Days when we were programming by hand. You cognize it’s thing you do, but it’s certainly not thing you do each the time, and it’s not thing you often did each the measurement up to the main usability astatine the top.
Change Isolation
We humans pervasively usage structured programming precisely because of its expertise to do this for astir function changes, to isolate higher functions from needing to cognize each detail of the little functions.
What would it moreover look for illustration for the top-most function to person to cognize “everything” that each the kid functions end up ever needing? For each usability parameter alteration successful the entire program to someway unit a alteration each the measurement up the full call stack?
Clearly this does not happen. It truthful thoroughly does not hap that you whitethorn beryllium having trouble moreover imagining what it is I americium talking about.
For instance, in the lawsuit of Go’s context, which I support bringing up not conscionable because Go is 1 of my main programming languages but because this is the example that keeps coming up, immoderate usability anyplace tin call context.Background() to get a degenerate, but afloat functional, context value.
So a usability that is changed to return a discourse can still beryllium called by immoderate different normal usability conscionable by passing the result of context.Background(). Moreover this is not a hypothetical situation; it is very communal for me, erstwhile I adhd a context.Context parameter to a usability that did not antecedently person one, to do exactly that successful existent code, to accommodate testing codification aliases different callers that don’t attraction astir the discourse to the caller parameter.
So this “linked list” narration successful believe doesn’t conscionable thin to break the concatenation earlier it hits main, it tends to break the concatenation very, very early. It hurts to person to thread changes each the measurement up, so we’ve developed a batch of ways of not having to do that and improving the encapsulation our functions provide. While you may person to do that sometimes, it’s the benignant of uncommon arena that is very cognitively available and easy to retrieve and notice, but arsenic a result, is wholly outsized in your memory. In reality it is simply a very uncommon case, not the communal one.
A normal alteration tin beryllium encapsulated. A very circumstantial alteration whitethorn for some reason request to spell each the measurement up to main, but this is intelligibly the exception, and moreover then, the remainder of the programme whitethorn usage this expertise to encasulate in different ways.
A colour alteration escapes this encapsulation. All functions supra it successful the stack must lucifer its requirements.
Color Definition
Any alteration to a usability pinch the “color” style of limitations is a color. Any alteration to a usability that is shaped for illustration the linked database is not a color.
While the original defining effort utilized async arsenic its canonical example, that does not mean each async successful each languages is ever a color. You must analyse the specifics of the language’s async support to see if it matches. In galore languages it is not a color. I deliberation with some existent changes to Zig a usability that does IO either is or is not a colour depending on which Io worth you walk to it, or possibly rather, the colour is an property of the Io worth alternatively than the usability itself. Async is simply a colour based connected whether it fits the definition; the definition is not that async is ever a color.
This is further analyzable by the truth that it isn’t ever quite as agleam and sparkling a statement arsenic we’d for illustration moreover successful the canonical “Javascript async” lawsuit the original effort is about. It is still possible to tally an async action successful a synchronous discourse is Javascript. It conscionable comes astatine the costs of the remainder of the async actions that should beryllium moving astatine the aforesaid time. I would see this “not really moving async astatine all”, fixed the staggering quality of the cost and really it destroys the basal functionality of the feature and conscionable hardly works. However, your mileage whitethorn vary. As with everything, the person you look, the much it seems to go a spectrum rather than a binary and the much analyzable the spectrum becomes.
However, conscionable because things are spectrums and drafting the statement ends up much analyzable than it whitethorn person initially seemed doesn’t mean there aren’t existent differences betwixt colour changes and normal changes.
Advanced Topic: Partial Coloring
For an precocious understanding, from this constituent of position we tin also imagine partial colors. Haskell is the king of partial colors, using them to make powerful guarantees about what the intervening functions tin do by utilizing the colour to constrain them somehow.
For instance, the Software Transaction Memory monad STM implements package transactional representation by allowing the creation of transactional values. These values tin only beryllium accessed via functions like readTVar from wrong an STM monad context.
An STM usability tin telephone a axenic usability which calls a axenic function, but if that axenic usability needs to past usage thing successful STM, that propagates up the full stack betwixt that telephone and the erstwhile use of STM, forcing the lifting of each intervening usability into the STM “color”. The colour is utilized to enforce the guarantee that only codification in the STM monad, moving done the STM machinery that guarantees transactionality of the result, tin entree transactional variables.
However, while that is simply a non-local, color-like relationship pinch some of the the functions supra it successful the telephone stack, it does not propagate each the way to the apical of the telephone stack. The type of a top-level Haskell program is IO (). STM does not look successful that type signature. Anything other than IO is guaranteed to beryllium encapsulated location successful the program beneath the main usability itself.
This is so a colour because this rides connected Haskell’s IO color; an STM transaction tin only itself beryllium done successful IO, truthful while the type system will let you to walk a TVar a to a axenic function, the pure function will not beryllium capable to do thing pinch it without unsafePerformIO. Haskell permits you to adhd “shades” of colour to IO that you tin enforce connected portions of your codebase.
Originally I tried to constitute this successful position of the simpler State but arsenic I was moving done it I realized that State is not a color. Nothing stops a usability from being passed the full state, calling immoderate different axenic usability pinch that state, and past that pure function tin conception its ain State-based computation pinch it and if you for illustration you tin thread that caller authorities each the measurement backmost to the original discourse pinch nary problems. That would beryllium klunky and unidiomatic, but it is some ineligible and moral.
So not each monads wrong of Haskell are colors, conscionable the ones that can only beryllium resolved wrong IO somehow. But Haskell does let you to create further “colors” pinch that mechanism.
I judge it is reasonable to opportunity that astir languages do not permit you to adhd your ain colors to it successful this manner. However, again, I would time off it to the experts successful each connection to statement whether or not this is the case.
To close, fto maine reiterate a colour must person this characteristic where it jumps complete intervening calls and forces the alteration connected each other functions wrong the applicable land of color. In the STM lawsuit above, anything that wants to entree the transactional variables must beryllium in the STM monad; the type checker will rigidly forestall immoderate effort to encapsulate the alteration to a information of the calls successful between, modulo unsafe. There is nary measurement for 1 of the axenic functions successful the middle to someway fulfill the STM constraint for everything beneath it (modulo unsafe), which is why it is reasonable to opportunity that it isn’t just being transmitted up arsenic a linked request but really directly imposed connected each intervening stack frames. If thing successful the middle can fulfill the request and encapsulate it from functions above it successful principle, moreover if for immoderate peculiar codification alteration it whitethorn not be applicable for a circumstantial instance, it is not a color.
English (US) ·
Indonesian (ID) ·