Gleam is a type-safe and extensible programming language available for the Erlang virtual machine and JavaScript runtime.
The recently released version 0.25 introduces a long-awaited new feature:use
expression(use
expressions).
According to the official introduction,use
expression is a syntactic sugar that turns all subsequent expressions into an anonymous function that is passed as an additional argument to the function call.
For example, suppose there is a function to be calledwith_file
which opens a file, passes the opened file to the given function so it can read or write to it, and closes the file.
// Define the function
pub fn with_file(path, handler) {
let file = open(path)
handler(file)
close(file)
}
// Use it
pub fn main() {
with_file("pokemon.txt", fn(file) {
write(file, "Oddish\n")
write(file, "Farfetch'd\n")
})
}
by usinguse
, this function can be called without additional indentation.Use belowuse
The example compiles to the exact same code as above.
pub fn main() {
use file <- with_file("pokemon.txt")
write(file, "Oddish\n")
write(file, "Farfetch'd\n")
}
And it’s not limited to a single argument, it can take functions of any argument, including functions that don’t take any arguments.
Release Notes | Release Notes
#Gleam #v025 #released #functional #programming #language #written #Rust #News Fast Delivery