Skip to content
HN On Hacker News ↗

Single file Gleam BEAM programs with escript | Gleam programming language

▲ 132 points 19 comments by figbert 3w ago HN discussion ↗

Pangram verdict · v3.3

We believe that this document is fully human-written

1 %

AI likelihood · overall

Human
100% human-written 0% AI-generated
SEGMENTS · HUMAN 5 of 5
SEGMENTS · AI 0 of 5
WORD COUNT 1,598
PEAK AI % 0% · §2
Analyzed
Jun 2
backend: pangram/v3.3
Segments scanned
5 windows
avg 320 words each
Distribution
100 / 0%
human / AI fraction
Verdict
Human
Pangram v3.3

Article text · 1,598 words · 5 segments analyzed

Human AI-generated
§1 Human · 0%

Gleam is a type safe and scalable language for the Erlang virtual machine and JavaScript runtimes. Today Gleam v1.17.0 has been published. Gleam Gathering But first: the first videos from the first ever all-Gleam conference have been released! You can view them on the Gleam Gathering YouTube account. The event was a roaring success and loads of fun. Stay tuned for news of the next one in 2027! Right, back to the release coverage. BEAM escripts When running on the Erlang virtual machine Gleam code is compiled to a series of .beam files, each of which contains the bytecode for a single Gleam module. This works fine for programs distributed or installed using package managers, containers, or other such systems, but having many files to share is a little inconvenient for sharing small command-line programs. In the JavaScript world this problem is solved using a "bundler", a program that takes many JavaScript modules and combines them into a single file. This single file can be copied to and run on any computer that has a JavaScript runtime installed (such as NodeJS, Deno, or Bun). Erlang has a similar solution: escripts. Much like a JavaScript bundle, an escript is a single file that contains all the modules of a program in the form of pre-compiled bytecode, and it can be run on any computer that has Erlang installed. The Erlang build tool has a convenient command for creating an escript, but for Gleam programmers the escript creation process has not been as straightforward. This release brings the gleam export escript command, which will compile the project, verify it has a valid main function, and build the escript file from the compiled bytecode. louis ~/src/my_project $ gleam export escript Compiling gleam_stdlib Compiling my_project Compiled in 0.48s

Your escript has been generated to /home/louis/src/my_project/my_project.

louis ~/src/my_project $ ./my_project Hello from my_project!

Highlight references Gleam's language server provides IDE functionality for all editors that implement the language server protocol.

§2 Human · 0%

This release adds support for the textDocument/documentHighlight feature, highlighting all references to a selected variable. For example, in this code triggering it with the cursor over any instance of vec will result in these highlights: fn to_cartesian(vec) { // ^^^ let x = vec.rho * cos(vec.theta) // ^^^ ^^^ let y = vec.rho * sin(vec.theta) // ^^^ ^^^ #(x, y) }

Thank you Gavin Morrow for this addition! Constant todo expressions Gleam's todo keyword is a placeholder expression that programmers can use when they have not-yet-finished code they wish to type-check or run. At compile time it outputs a warning to say that the code is incomplete, and if the code path with the todo expression is run then it will panic, exiting the program. todo can now also be used in constant expressions. Since constant expressions are evaluated at compile time, when todo is used in a constant the program can no longer be run, but it can still be type checked and analysed. This has also enabled us to upgrade the "Fill labels" code action so that it works with constants too. When run it fills in the missing labelled arguments from a record constructor. For example: pub type Pokemon { Pokemon(number: Int, name: String, hp: Int) }

pub const cleffa = Pokemon(number: 173)

In this code snippet we haven't specified the name and hp fields, that's an error! Triggering the "Fill labels" code action will result in the following: pub const cleffa = Pokemon(number: 173, name: todo, hp: todo)

Thank you Giacomo Cavalieri! Record update hovering Hovering in your editor is a great way to gleam more information, with the language server showing the types, documentation, and other details of whatever is being hovered over. Gleam's record update syntax is used to create a new record from an existing one, but with some fields updated with new values. When hovering over one of these the language server will now also show the remaining fields that have not been given new values, saving you from navigating to the definition to see what others you could set new values for.

§3 Human · 0%

pub type Person { Person(name: String, age: Int) }

pub fn happy_birthday_mom() { let mom = Person(name: "Antonella", age: 60) Person(..mom, age: 61) // ^^^^^ Hovering this will show: // Unchanged fields: // - name }

Thank you Giacomo Cavalieri! Unknown value import suggestions In Gleam functions from other modules are almost always used in a qualified fashion, written as dict.fold rather than just fold. This is done so it is clearer to the reader where the function is defined and what it does, and to prevent redundant suffixes being added to function names to indicate what type they work with. Sometimes the programmer may forget to write the module qualifier, resulting in a compile error due to there being no value with that name in scope. When this happens the compiler will now search in the imported modules for an appropriate value with that name, and suggest it as a position correction. For example, for this invalid program: import gleam/io

pub fn main() -> Nil { println("Hello, World!") }

The compiler will display this error message: error: Unknown variable ┌─ /path/to/project/src/project.gleam:4:3 │ 4 │ println("Hello, World!") │ ^^^^^^^

The name `println` is not in scope here. Did you mean one of these:

- io.println

Thank you raphrous! Context aware type printing in warnings Up until now when the name of a type is displayed in a warning it has been displayed using its canonical name, but this may not be how the programmer has referred to the type in the code. A type could be referred to in a qualified way such as accounting.Invoice, or the programmer could have aliased the type to a new name to make it easier to understand in the context of that code. Types in warnings are now correctly qualified or aliased when displayed in warnings.

§4 Human · 0%

For example, this code: import user as visitor

pub fn main() { user.to_string(todo) |> io.println }

Will produce the following warning: warning: Todo found ┌─ /src/warning/wrn.gleam:4:19 │ 4 │ user.to_string(todo) │ ^^^^ This code is incomplete

This code will crash if it is run. Be sure to finish it before running your program.

Hint: I think its type is `visitor.User`.

Notice how the type hint is correctly qualified for the module the warning is raised in. Thank you Giacomo Cavalieri! JavaScript pattern matching optimisations Gleam's case expression pattern matching may look like a linear search from the first to the last clause, but it compiles to an efficient decision tree that takes a "divide and conquer" approach to finding the matching clause. Daniele Scaratti and Gavin Morrow have worked on further optimising the code generated for pattern matching when compiling to JavaScript, detecting and removing some redundant checks when working with the length of bit arrays, and making the code for assignments more compact. Thank you both! Quiet development When running the Gleam build tool it will print messages that show what it is doing. louis ~/src/my_project $ gleam dev Compiling gleam_stdlib Compiling my_project Compiled in 0.48s Running my_project_dev.main Hello, from my_project_dev!

The --no-print-progress flag can be used to disable this information, and with the release it is now accepted by the gleam dev command, which runs code in development mode. louis ~/src/my_project $ gleam dev --no-print-progress Hello, from my_project_dev!

Thank you Giacomo Cavalieri! Outdated dependencies count The gleam deps outdated command is used to see which dependencies of a package have newer versions that could be adopted. With this release it now includes a summary showing how many packages have newer versions available. For example: louis ~/src/my_project $ gleam deps outdated 1 of 12 packages have newer versions available.

§5 Human · 0%

Package Current Latest ------- ------- ------ gleam_stdlib 0.70.0 0.71.0

It is also helpful when no packages are outdated, as before some people were confused by the lack of anything being printed and were unsure if they had any outdated dependencies or not. louis ~/src/my_project $ gleam deps outdated 0 of 12 packages have newer versions available.

Thank you Daniele Scaratti! Better git repository detection The gleam publish command now has better Git repository detection in monorepos, correctly identifying if the package is in a git repository even if the Gleam package being published is not located at the root of the repository. Thank you Andrey Kozhev! Further fault tolerance Gleam's compiler implements fault tolerant compilation, enabling it to analyse and infer information about code even when it is in an invalid state. This is useful as it means the language server is always able to provide useful help to the programmer. Giacomo Cavalieri has improved the fault tolerance for errors in the list-prepend syntax and the record update syntax. Thank you! Remove redundant record update code action The language server now has a code action to remove a redundant record update. For example: pub type User { User(name: String, likes: List(String)) }

pub fn main() { let lucy = User(name: "Lucy", likes: ["Gleam", "Ice Cream"]) let jak = User(..lucy, name: "Jak", likes: ["Gleam", "Dogs"]) // ^^^^^^ This record update is not needed! }

This record update is not actually needed and will raise a warning; all fields are already specified. Triggering the code action anywhere on the expression will remove the unnecessary update: pub type User { User(name: String, likes: List(String)) }

pub fn main() { let lucy = User(name: "Lucy", likes: ["Gleam", "Ice Cream"]) let jak = User(name: "Jak", likes: ["Gleam", "Dogs"]) }

Thank you Giacomo Cavalieri! Correct operator in guard expression code action Gleam doesn't have overloading for its operators, so each one works with a specific type.