DEV Community

Mixing Gleam & Elixir

Michael Jones on March 20, 2020

Update: This was very much an initial exploration. There is now an official project with a mix_gleam plugin. Please check that out! At Contact ...
Collapse
 
chazsconi profile image
chazsconi

Nice article. This has allowed me to add a bit of gleam to my existing Elixir project.

I found that if I want to use gleam libraries I need to put them in a rebar.config in the project root so that the project compiles, but then also add them to the mix.exs so that they are available at run time. Do you have any ideas for avoiding this duplication?

Collapse
 
michaeljones profile image
Michael Jones

Thanks for the comment. I'm sorry to say that I've not gone much further on this so I actually can't help you with your issue. I'm excited by Gleam but I'm still trying to figure out how to fit it into my projects.

I asked on the Elixir Forum Gleam thread about further interop between Phoenix, Ecto & Gleam but lpil, the Gleam creator, suggests that this might be quite hard due to Elixir macros. I'm keen to explore more but I haven't found the time.

Collapse
 
chazsconi profile image
chazsconi

OK - thanks for the reply! I'll let you know if I find out more myself.

Thread Thread
 
crowdhailer profile image
Peter Saxton

I contributed a fix to Gleam that means it now finds gleam source for dependencies in a mix project github.com/gleam-lang/gleam/pull/596

Also instead of defining your own compiler module an alias will do aliases: ["compile.gleam": "cmd gleam build"]

I put a working example here github.com/midas-framework/elixir_...

Thread Thread
 
chazsconi profile image
chazsconi

Great! Works perfectly. Thanks Peter!

Thread Thread
 
crowdhailer profile image
Peter Saxton

There is now. github.com/gleam-lang/mix_gleam

Which should work even better.

I've also updated the elixir with gleam example

Collapse
 
preslavrachev profile image
Preslav Rachev

Nice writeup! Would you know if it is possible to register Gleam modules to be accessed using the same naming convention of Elixir? I know that at the end of the day, all module names are just atoms, but mixing the style will make the code base inconsistent, IMO.

Collapse
 
michaeljones profile image
Michael Jones

Thanks for the question. I don't know too much about the situation, I'm afraid.

I think Gleam modules are compiled to be maximally accessible from Erlang. It has crossed my mind that Gleam could have an output mode that namespaced things for Elixir (I think Elixir modules are just atoms that start with "Elixir." though I'm far from an expert.) I don't think that is planned at the moment though. Might be worth raising. The gleam community has a uses GitHub discussions (github.com/gleam-lang/gleam/discus...) and Discord (discord.com/invite/Fm8Pwmy) for communicating mostly at the moment.

I imagine that if you were to create a library in Gleam and wanted to make it even friendlier for Elixir programmers then you publish some Elixir modules into the bundle that act like a shim to provide Elixir style access to the functions and types.

Collapse
 
eterps profile image
Erik Terpstra
Collapse
 
valpackett profile image
Val Packett

huh, turns out it's pretty easy to add compilers to Mix.

But I guess the generated erlang files would be in the same directory with this setup.. a proper integration would hide them in _build

Collapse
 
michaeljones profile image
Michael Jones

Yeah, not that hard at all, eh? :)

Yeah, the Gleam compiler would have to be tweaked so it was at least configurable to target the _build folder instead. The creator of Gleam is interested in that route but is unsure of how best to approach it: github.com/gleam-lang/gleam/issues...

Collapse
 
fibric profile image
Denny Trebbin

Good one! I like Gleam and watching it since a while. Now I have a good reason to use it :)

Collapse
 
szajbus profile image
Michał Szajbe

Did you use Gleam for some specific problem that would be more difficult to solve using just Elixir?

Collapse
 
michaeljones profile image
Michael Jones

Thanks for commenting. Honestly, no. I haven't written much in Gleam at all. Given that it targets the BEAM I imagine it will one day be good for similar problems as Elixir. At the moment it is quite young and I struggle to figure out how best to use it. My main interest is that I really enjoy working with Elm as a simple, clear language with a strong static type system and a friendly compiler and to the degree that Gleam has similar goals but targets the Erlang VM I would like to see it succeed.