3 ms·
Isn't use part of Elixir? At least I saw it in Elixir in Action, use GenServer or something...
by k__ 2y ago
Isn't use part of Elixir?
At least I saw it in Elixir in Action, use GenServer or something...
- aeturnum 2y agoI don't know how use works in gleam but, at first brush they seem very different. Use in elixir basically allows you to execute a macro callback in the module `use`ing the other module. In gleam it seems to be a tool for chaining function calls (maybe closer to `with` in elixir?).
- behnamoh 2y agoYes, Gleam's "use" is similar to "with" in Koka and some other langs. It's really not the same "use" we are used to (pun intended).
- asa400 2y agoCorrect, `use` is a part of Elixir. When you `use` a module like `use FooModule`, it runs the `__using__` macro defined in `FooModule`. Because `__using__` is a macro, it serves as an extension point that allows modules that define it to "inject" code into the modules that `use` it. This injected code could be anything: function definitions, constants, module definitions, etc. 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#use https://hexdocs.pm/elixir/alias-require-and-import.html#use
- m8s 2y agoTwo quite different things! Elixir’s use: https://hexdocs.pm/elixir/main/alias-require-and-import.html#use https://hexdocs.pm/elixir/main/alias-require-and-import.html... Gleam’s use: https://tour.gleam.run/advanced-features/use/ https://tour.gleam.run/advanced-features/use/