Follow us
Breaking
Tech News

Type safety and mathematical logic in Gleam

Gleam offers a simpler type system that catches errors Elixir misses, such as handling division by zero by returning zero instead of crashing. While Gleam provides superior type safety for business logic, Elixir maintains a massive advantage through its mature macro system and ecosystem.

Share

Gleam provides a simpler type system that catches many errors found in Elixir. This functionality attracts developers who prioritize enforcing invariants within core business logic. The difference in how they handle division by zero is significant because Gleam returns zero for division by zero, a behavior taken from Pony, Coq, and Lean, whereas Erlang results in a crash. Gleam also uses immutable single-linked lists. These lists allow for efficient addition and removal of elements from the front, though counting the length of a list or accessing other positions requires more effort. Developers can call functions via the package name or use imports such as import gleam/package.{function} to use a function without its package name. Gleam uses the let keyword for assignments and follows snake_case for variables. Users can use type annotations, such as let _is_cool: Bool = True, to define variables. The language also supports the <> operator for string concatenation. The io.debug function is specifically designed for pipelines, as it yields the value after the print operation is complete. In contrast, io.println writes a string to standard output. Gleam lists are generic types that contain a type parameter for the values they hold, such as List(Int) or List(String). Do you think these syntax choices make Gleam easier to learn?

Macro Limitations and the Ecosystem Gap

Elixir maintains a much larger ecosystem than Gleam. Many developers hesitate to switch to Gleam because they must invent many of the tools they need or wait for others to build them. This gap exists because Elixir has a staged macro system where macros run in stages. Gleam lacks the ability to execute code at compile time. This restriction makes a lisp style macro system impossible for Gleam at this time, though developers could potentially use a web assembly backend or a Gleam interpreter to address this in the future.

Feature Gleam Behavior Elixir Behavior
Division by zero Returns zero Crashes
Macro execution No compile-time execution Staged execution
Ecosystem scale Smaller Much larger

The 1.20 release for Elixir addresses some major problems within that language. Even with these improvements, the ecosystem remains a massive advantage for Elixir. One user mentioned that Elixir makes it intuitive to represent different implementations via one struct per implementation and polymorphic behavior by protocol. This makes it easier to represent a system where different implementations might not fill all the fields of a struct. In Elixir, individually checking every field becomes difficult when implementations decide to fill certain fields or not. In the discussion of macro systems, researchers point to OCaml as an alternative. OCaml has several systems, such as Caml5p, which is a preprocessor that is not as type safe or pure in OCaml syntax. Others prefer PPX style staged macro implementations. These implementations are like Elixir’s, where macros are run in stages. This resemblance to OCaml’s staged macro systems provides a potential path for Gleam’s future development. The lack of a native macro system in Gleam means developers face a higher burden of manual implementation for repetitive tasks.

Integration and User Choice

Developers often consider a hybrid approach to solve the tension between these two languages. This strategy involves using Gleam for business logic to enforce invariants through the type system. Developers then use Elixir to access the wider ecosystem and maintain tighter BEAM integration. This prevents the need to abandon the well-maintained features of the BEAM runtime.

User Priority Preferred Language
Type Safety Gleam
Ecosystem Access Elixir
BEAM Integration Elixir

One developer noted that Gleam shines for API and web applications because the garbage collector hides complexity. They found the type system in Gleam to be very good and capable of catching errors that they usually face in Elixir. However, they still find Elixir more attractive for its mature ecosystem. Many users refuse to abandon the well-maintained Elixir ecosystem for a new language. They view themselves as no longer being the early adopter type. This preference stems from the difficulty of having to invent much of the goodies in the ecosystem when using Gleam. One user highlighted the difficulty in Elixir of representing a struct where different implementations may not fill all the fields. They noted that Gleam’s type system handles these scenarios much better. Some developers compare the trade-offs to Rust, which they find amazing for its type system but too low level for high level applications. The comparison between Gleam and Elixir shows a divide between those seeking strict type safety and those needing broad library support. Gleam provides the type safety users want, but Elixir provides the tools they need. Will Gleam eventually develop the necessary tools to compete with the mature Elixir ecosystem?

Share

Technewsdaily

Senior tech writer covering AI, gadgets and cybersecurity. Breaking down the news that matters, every day.