July 23rd, 2024

Enhancing Your Elixir Codebase with Gleam

Gleam is a statically typed language for the BEAM platform, enabling type safety in Elixir projects. The article details integrating Gleam for managing student enrollments, emphasizing testing and robustness.

Read original articleLink Icon
Enhancing Your Elixir Codebase with Gleam

Gleam is a statically typed language that runs on the BEAM platform, allowing developers to enhance their Elixir codebases without complete rewrites. This article discusses integrating Gleam into an Elixir project to leverage type safety, particularly for critical applications like payment processing systems. The integration process involves setting up a Phoenix and Ecto application, installing the mix_gleam library, and modifying the project configuration to accommodate Gleam code.

The example project focuses on managing student enrollments in university courses, implementing business rules such as age restrictions and waitlist management. The initial setup includes creating necessary schemas and migrations for students and courses. The Gleam code defines types for students and courses, along with an enrollment function that checks age requirements and seat availability.

Unit tests are written in Gleam to validate the enrollment logic, demonstrating the benefits of type safety by catching errors during testing. The article also explains how to call Gleam functions from Elixir, emphasizing the need to convert data into a format that Gleam can interpret. This integration allows developers to maintain Elixir's dynamic features while benefiting from Gleam's static typing, ultimately enhancing the robustness of their applications. The article concludes by encouraging further testing and exploration of Gleam's capabilities within Elixir projects.

Link Icon 4 comments
By @behnamoh - 3 months
Gleam is beautifully designed, but I don't really understand the concept of "use". I've read every article and documentation I could see about it, but it still doesn't "click". Perhaps using another keyword (instead of "use") would work better?

In general, I find "use" a big deviation from Gleam's philosophy. Everything else in the language is well-defined and serves a specific purpose. "use", however, serves several goals, and thus is ambiguous (in the sense that it doesn't have one well-defined definition). [0, 1]

[0]: https://www.reddit.com/r/ProgrammingLanguages/comments/19ctw...

[1]: https://erikarow.land/notes/using-use-gleam

By @oDot - 3 months
I have been using Gleam on the frontend in production, together with Vue[0] and I can confirm it's a joy to use.

Simplicity is such an advantage for a young language.

[0] https://github.com/vleam/vleam

By @ashton314 - 3 months
Ah, the joys of having a compatible data model! I think it’s really cool that Gleam and Elixir can interop so well because they’re both running on the BEAM and can share data structures.

I’m curious to see how Elixir’s very recent (still prototypical) type system addition will change the demand for a language like Gleam.