Consider using Elixir style maps instead of records in Gleam to erlang transpilation #1298
Replies: 4 comments 5 replies
-
Hello! Thanks for the suggestion. I agree that improving the interop story with Elixir would be greatly beneficial. It's quite late to be making any large breaking changes to the core language, so we would need a clear reason to do so. I see the following advantages
and the following disadvantages
I think the struct interop situation would not improve as much as you might want it to because while we would have a map, we would not have an Elixir compatible struct. A struct definition in Elixir also adds functions to the containing module, something we cannot do in Gleam as we can have as many custom types per module as you want. While discussing this it would be good to also think about what other solutions might exist for this Elixir struct <-> Gleam record awkwardness. Perhaps some constructor functions in the generated Erlang pub type Cat {
Cat(name: String, age: Int, is_cool: Bool)
} -module(mymod).
-export([catFromMap/1, catToMap/1]).
catFromMap(#{name => Name, age => Age, is_cool => IsCool}) ->
{cat, Name, Age, IsCool}.
catToMap({cat, Name, Age, IsCool}) ->
#{name => Name, age => Age, is_cool => IsCool}. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your reply. I like the generated constructor functions idea i.e. |
Beta Was this translation helpful? Give feedback.
-
I think the closer we get to 1.0 the more important elixir interop is... Just my 2 cents: The easier it is to drop in Gleam into Elixir projects the more growth we might have there? |
Beta Was this translation helpful? Give feedback.
-
two alternatives that would be good considering:
both would allow the programmer to decide what datastructure representation is better to their use case, being on par with erlang's exposed api (records and maps up to choose) besides interop, another main benefit is that data upgrades are easier if the backing type is a map |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Sorry if this issue has already been considered and definitively decided. In that case please feel free to close the ticket
I understand that gleam uses erlang style records extensively when transpiling to erlang.
Elixir uses erlang maps as a way to define structs. I think there are certain advantages to using maps -- when you see the key
__struct__
while tracing you know for sure that it corresponds to an Erlang struct. The same cannot be said about records in erlang -- you cannot be totally sure if a tuple corresponds to a erlang record for sure. Also the keys in a map make understanding things much simpler during tracing/debugging. Whereas in a tuple you need to keep referring back to the record definition, especially if the record has many fields.Is it too late to consider moving to the usage of maps in gleam transpilation? I'm guessing that maps are bit more resource hungry but there are ecosystem and debugging benefits to using them...
I'm sure a lot of the gleam user base will be coming from Elixir rather than Erlang. Currently to interoperate with Elixir better they might need to use something like https://github.com/QuinnWilton/strucord . We could avoid that entirely if gleam used maps under the hood instead of records. I have very little idea of how that will all work out practically as I've just started exploring gleam.
(I'm also taking the liberty of cc-ing @QuinnWilton in case there are any opinions on this topic -- Thanks in advance!)
Beta Was this translation helpful? Give feedback.
All reactions