Enhancing Your Elixir Codebase with Gleam
blog.appsignal.com
blog.appsignal.com
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...
Exactly! It's one of the rare ugly parts of Gleam, and I sincerely hope "use" doesn't catch on (it seems like many people find it inconsistent and won't use it anyway).
I fully agree it's unexpected that everything after the use statement becomes part of the body of the use statement.
I find that `use` is quite effective when needed. This is the part referenced in her article where I find `use` particularly helpful. Something like:
fn outer() -> Result(success, failure) {
result.map(parse_id(), fn(id) {
... // More code that uses the id
})
}
Becomes: fn outer() -> Result(success, failure) {
use id <- result.map(parse_id())
... // More code that uses the id
}
In more concrete terms, I’m building an application with Gleam and Wisp that uses Wisp’s `require_form`. The type signature for `require_form` is: pub fn require_form(
request: Request(Connection),
next: fn(FormData) -> Response(Body),
) -> Response(Body)
But I get to use it like this: use form <- wisp.require_form(req)
I’m not sure if I have the appropriate nomenclature here, and I consider myself a beginner of writing strongly typed functional languages, but when I use `use`, I get to invoke the callback with the value on the left side of the arrow.As Erika so thoughtfully put it:
> The key is to use `use` when it allows you to highlight the happy path of your code … A use expression is syntax sugar, and it’s always possible to write Gleam code without it, though maybe not as clearly.
You can see more here [1] if you want, but note that there are a lot of structural changes in this codebase while I learn and figure out how to best use Gleam and Wisp :)
1. https://github.com/usepriceflow/app/blob/main/src/app/web/ad...
At least I saw it in Elixir in Action, use GenServer or something...
For example:
defmodule A do
defmacro __using__(_env) do
quote do
defmodule Math do
def plus_itself(y) do
y + y
end
end
end
end
end
defmodule B do
use A
end
B.Math.plus_itself(10) |> IO.inspect() #=> 20
More info:
https://hexdocs.pm/elixir/alias-require-and-import.html#useElixir’s use: https://hexdocs.pm/elixir/main/alias-require-and-import.html...
Gleam’s use: https://tour.gleam.run/advanced-features/use/
Simplicity is such an advantage for a young language.
I’m curious to see how Elixir’s very recent (still prototypical) type system addition will change the demand for a language like Gleam.
I don’t think that Elixir’s types and Gleam’s have much in common, they will provide very different experience. Coupled with there not being much crossover from the Elixir community to the Gleam one I don’t think it’ll have much impact on Gleam.
First, let me make it clear: I'm not trying to knock Gleam and it's type system—it's really cool what you've done.
> Not much crossover from the Elixir community
Interesting! I didn't know that. For the die-hard ML-style type system fans (of which I am one) Gleam will absolutely be the most attractive thing on the BEAM block. I think there is a small contingent of developers who would really rather have a type system who might choose Gleam, but that Elixir's new type system (when it matures to support user-supplied annotations and whatnot) might suffice. I think it's an interesting case to see how adding a type system might shift people's preferences between two established languages.
That said, again please don't see this as a knock on Gleam or me trying to say "well now Gleam is obsolete"—I'm not trying to say that. I hope Gleam has an illustrious future. :)
Elixir and Gleam are very different languages offering very different things.
But most importantly, Elixir is, and always will be, a gradually typed language. There will always be parts of it that will remain or encourage dynamic typing and that opens the door to ambiguity. In a statically typed language the compiler enforces type annotations and that is where Gleam shines.