July 8th, 2024

Using Use in Gleam

Erika Rowland, erikareads, explains the benefits of the "use" expression in Gleam v0.25, replacing "try" for clearer and concise code. It simplifies callback functions, enhances readability, and streamlines error handling.

Read original articleLink Icon
Using Use in Gleam

Erika Rowland, also known as erikareads, discusses the use of the "use" expression in Gleam programming language. The "use" expression was introduced in Gleam v0.25 as a replacement for the "try" keyword. It allows for a more concise and clear way of writing functions that involve callback functions as their last argument. By using "use," the code structure becomes more streamlined, especially in error handling and context management scenarios. The article provides examples of how "use" can simplify code by avoiding nested callback functions and improving readability. While "use" is considered syntax sugar and not essential, it can enhance code clarity by focusing on the main logic of the program. The choice of "use" as a keyword was a result of discussions within the Gleam community to introduce a general syntax sugar. Overall, the use of "use" in Gleam can help developers emphasize the main flow of their code while addressing error handling and context management concerns effectively.

Link Icon 3 comments
By @h506001 - 3 months
Amazing write up as usual, Erika. Your blog is an inspiration and model of what a tech blog should look like: high-quality info, plain and clear language, and extreme focus.
By @taxyneno - 3 months
Gleam has a lovely, friendly community.
By @constantcrying - 3 months
I would highly recommend not writing a tutorial in terms of what "I" did and what "I" want to do, but rather about what "we" are doing or what allows "us" to do certain things.

Surely it isn't intended that way, but framing everything as what "I" am doing comes off as pretty narcissistic.