The color of Composable functions 🖍️ (2024)

The color of Composable functions 🖍️ (1)

Composable functions have different limitations and capabilities than standard functions. They have a different type, and model a very specific concern. This differentiation can be understood as a form of “function coloring”, since somehow they represent a separate category of functions.

Function coloring

“Function coloring” is a concept explained by Bob Nystrom from the Dart team at Google in a blockpost called “What color is your function?”, written in 2015. He explained how async and sync functions don’t compose well together, since you cannot call async functions from sync ones, unless you make the latter also async, or provide an awaiting mechanism that allows to call async functions and await for their result. This is why Promises and async/await were introduced by some libraries and languages. It was an attempt to bring composability back. Bob refers to these two function categories as two different “function colors”.

In Kotlin, suspend aims to solve the same problem. However, suspend functions are also colored, since we can only call suspend functions from other suspend functions. Composing programs with a mix of standard and suspend functions requires some ad-hoc integration mechanism (coroutine launch points). The integration is not transparent to the developer.

Overall, this limitation is expected. We are modeling two categories of functions that represent concepts of a very different nature. It’s like speaking two different languages. We have operations that are meant to calculate an immediate result (sync), and operations that unfold over time and eventually provide a result (async), which will likely take longer to complete.

Effective Android is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.

Coloring in Compose

In Jetpack Compose, the case of Composable functions is equivalent. We cannot call Composable functions from standard functions transparently. If we want to do that, an integration point is required (e.g: Composition.setContent). Composable functions have a completely different goal than standard functions. They are not designed to write program logics, but to describe changes for a node tree.

It might seem that I am tricking a bit here. One of the benefits of Composable functions is that you can declare UI using logics, actually. That means sometimes we need to call Composable functions from standard functions. For example:

@Composablefun SpeakerList(speakers: List<Speaker>) { Column { speakers.forEach { Speaker(it) } } }

The Speaker Composable is called from the forEach lambda, and the compiler does not seem to complain. How is it possible to mix function colors this way then?

Inline

The reason is inline. Collection operators are declared as inline, so they inline their lambdas into their callers making it effectively as if there was no extra indirection. In the above example, the Speaker Composable call is inlined within the SpeakerList body, and that is allowed since both are Composable functions. By leveraging inline we can bypass the problem of function coloring to write the logic of our Composables. Our tree will be comprised of Composable functions only.

But, is coloring really a problem?

Well, it might be if we needed to combine both types of functions and jump from one to the other all the time. However, that is not the case either for suspend or @Composable. Both mechanisms require an integration point, and therefore we gain a completely colored call stack beyond that point (everything suspend, or Composable). This is actually an advantage, since it allows the compiler and runtime to treat “colored” functions differently, and enable more advanced language features that were not possible with standard functions.

Language features

In Kotlin, suspend allows to model async non-blocking programs in a very idiomatic and expressive manner. The language gains the ability to represent a very complex concept in an extremely simple way: adding a suspend modifier to our functions. On the other hand, @Composable makes standard functions become restartable, skippable, and reactive, which are capabilities that standard Kotlin functions do not have.

Learn more đź“–

This post is part of the first chapter of the Jetpack Compose internals book. If you want to learn more about Composable functions, the Compose compiler, runtime, and Compose UI, that might be a purchase you’d want to consider.

Jetpack Compose internals đź“–

👨‍🏫 Online training

You might want to consider attending the next edition of the highly exclusive “Jetpack Compose and internals” online training I’m giving in February. I have carefully crafted it so attendees can master the library from the Android development perspective, while learning about its internals in order to grow a correct and accurate mental model. I’ll be your teacher 🙌

This course will allow you to position yourself well in the Android development industry. Earlybird seats still available.

Enroll here

The color of Composable functions 🖍️ (2024)
Top Articles
2 of the Most Powerful Scriptures About Wealth-Building – Workmind – Blog
12 Easy Ways to Fix Google Drive Upload Failed Issue in 2024
Creepshotorg
Xre-02022
CLI Book 3: Cisco Secure Firewall ASA VPN CLI Configuration Guide, 9.22 - General VPN Parameters [Cisco Secure Firewall ASA]
More Apt To Complain Crossword
Weather In Moon Township 10 Days
Prices Way Too High Crossword Clue
Helloid Worthington Login
South Bend Tribune Online
Watch TV shows online - JustWatch
Alaska: Lockruf der Wildnis
How to find cash from balance sheet?
Illinois Gun Shows 2022
iLuv Aud Click: Tragbarer Wi-Fi-Lautsprecher fĂĽr Amazons Alexa - Portable Echo Alternative
What is Rumba and How to Dance the Rumba Basic — Duet Dance Studio Chicago | Ballroom Dance in Chicago
Wicked Local Plymouth Police Log 2022
Urban Airship Expands its Mobile Platform to Transform Customer Communications
Schedule 360 Albertsons
Concordia Apartment 34 Tarkov
Pecos Valley Sunland Park Menu
Melendez Imports Menu
1973 Coupe Comparo: HQ GTS 350 + XA Falcon GT + VH Charger E55 + Leyland Force 7V
Optum Urgent Care - Nutley Photos
Gotcha Rva 2022
Airtable Concatenate
Foodsmart Jonesboro Ar Weekly Ad
Snohomish Hairmasters
Lovindabooty
Maths Open Ref
Till The End Of The Moon Ep 13 Eng Sub
Best New England Boarding Schools
Mumu Player Pokemon Go
Word Trip Level 359
Ni Hao Kai Lan Rule 34
Seymour Johnson AFB | MilitaryINSTALLATIONS
Mississippi State baseball vs Virginia score, highlights: Bulldogs crumble in the ninth, season ends in NCAA regional
Why Holly Gibney Is One of TV's Best Protagonists
Puffco Peak 3 Red Flashes
1v1.LOL Game [Unblocked] | Play Online
Fetus Munchers 1 & 2
Riverton Wyoming Craigslist
Home Auctions - Real Estate Auctions
Craigslist Food And Beverage Jobs Chicago
LoL Lore: Die Story von Caitlyn, dem Sheriff von Piltover
Smite Builds Season 9
Craigslist/Nashville
Craigslist St Helens
What is a lifetime maximum benefit? | healthinsurance.org
Wzzm Weather Forecast
Gummy Bear Hoco Proposal
Access One Ummc
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 5835

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.