#Nostrum with Gleam

1 messages · Page 1 of 1 (latest)

normal mural
#

https://github.com/Kraigie/nostrum

On a very basic level, how would I convert the code in the picture to Gleam bindings? I know how to bind functions like create_message. As far as I know, Gleam does not use the use keyword, nor does it have an alias keyword. I'm assuming they are syntactic sugar, though I don't know how to deal with them in Gleam.

tired surge
#

It's not possible to directly convert that code as Gleam doesn't have first class modules

#

Or rather, you could directly use that API but Gleam wouldn't be able to verify the types etc are correct for each function

#

alias Nostrum.Api is just "any time there is Api I mean Nostrum.Api"

#

use is calling the __using__ macro in Nostrum.Consumer. You'll need to read what it does an reverse engineer if you want to use this directly from Gleam as Elixir macros cannot be used from other languages

#

Most the time macros are added without any good reason and can be avoided

#

For this library I'd probably write the code that uses this library in Elixir and then have it call your Gleam code

normal mural
#

Ok thanks 👍

normal mural
#
defmacro __using__(_opts) do
    quote location: :keep do
      use GenServer

      @behaviour Nostrum.Consumer

      @before_compile Nostrum.Consumer

      def child_spec(opts) do
        %{
          id: __MODULE__,
          start: {__MODULE__, :start_link, [opts]},
          type: :worker,
          restart: :permanent,
          max_restarts: 0,
          shutdown: 500
        }
      end

      def start_link(opts) do
        GenServer.start_link(__MODULE__, [], opts)
      end

      @impl GenServer
      def init([]) do
        ConsumerGroup.join(self())
        {:ok, nil}
      end

      @impl GenServer
      def handle_info({:event, event}, state) do
        Task.start_link(fn ->
          __MODULE__.handle_event(event)
        end)

        {:noreply, state}
      end

      defoverridable GenServer
    end
  end```
#

This is the __using__ function

#

Would I have to write bindings for all of that in Gleam then call all of that?

tired surge
#

The other way around. Are you familiar with Elixir macros?

normal mural
#

No not really. I just resorted to using Elixir

#

I didn't know Elixir actually has some low-level form of type checking

#

If you put the wrong type for something it gives you an error

tired surge
#

It has some extremely limited type checking

#

Practically speaking it doesn't really have any, but they're working on it

#

They've got a design for an actual type system to replace this previous unfinished attempt, but it's unclear whether they will be able to merge it or not yet

#

It's going well though

normal mural
tired surge
#

Not at all

normal mural
#

Or, at the very least, make Gleam less appealing of an option

tired surge
#

If they succeed it'll be wildly different experience to Gleam, like comparing Python and OCaml

normal mural
#

What does Gleam have that Elixir doesn't besides static types?

tired surge
#

It'd be like saying Elixir replaces Java as they both have functions

#

Gleam's type system is much stronger and easy to use than Elixir's proposed one, if they do get it.

#

Gleam's main strengths over Elixir are probably simplicity, ease of learning, ease of refactoring, compilation speed, tooling, and that it can compile to JavaScript also.

#

Not as clearly a strength, I personally prefer Gleam as a language to Elixir