#How are types converted between Erlang and Gleam

1 messages · Page 1 of 1 (latest)

upper laurel
#

Firstly, I the only time i ever wrote Erlang code is the snipped ill present:

-type result() :: {foo, [string]} | bar.

readlines(FileName) ->
    case file:open(FileName, [read]) of
        {ok, Device} ->
            try
                Content = get_all_lines(Device),
                {foo, Content}
            after
                file:close(Device)
            end;
        {error, Reason} ->
            bar
            % []
    end.

Given this Erlang code and this Gleam type:

type ErlangResult {
  Bar
  Foo(List(String))
}

it appears to magically work. Is there some ressource (maybe its how types are represented in the BEAM?) for which I can learn more about type conversions and what works when?

full socket
#

The type annotation for your Erlang function isn’t correct there

#

It says it returns a list of the atom “string”

#

When you want it to be binary()

#

Erlang types are not checked in any way by Gleam so you could put anything you want as the Erlang type and it’s be OK

#

The Gleam type is incorrect because- it may not be valid unicode so it should be BitArray

#

As for conversions; there are none! Whatever your external code returns is used as-is, so it’s really important that your annotations are accurate

upper laurel
#

Yeah I realised I dont even need the type in Erlang. I came to learn gleam, but now I wanna learn both haha

upper laurel
full socket
#

Erlang is cool

upper laurel
full socket
#

Nothing is converted, that’s the same thing in the syntax of each language

upper laurel
#

its probably the representation in the beam, so i might look that up?