Erlang external function with union return type #5114
-
|
I've been playing around with Gleam and enjoying it so far! However, I've run into some pain points when attempting interop with Erlang. My understanding from the docs is that Erlang atoms and tagged types can be modelled using Gleam's For example, I've attempted to model this as: import gleam/erlang/charlist.{type Charlist}
pub fn main() -> Nil {
get_line(charlist.from_string("Hello: "))
}
@external(erlang, "io", "get_line")
fn get_line(prompt: Charlist) -> GetLineResponse
type GetLineResponse {
Data(Charlist)
Eof
Error(ErlangAny)
}
type ErlangAnyThis doesn't work and I end up with an error like: Any thoughts on what I'm doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
I would also recommend converting from a charlist to a binary string so you can perform regular Gleam string operations on the output. If you just want to get user input, you can also use the |
Beta Was this translation helpful? Give feedback.
Data(Charlist)would be represented as{data, "..."}, which is not what the code is actually returning. Instead what you should do is write an erlang wrapper which callsget_lineand returns something which can be represented as a Gleam type. The externals documentation may be of help: https://gleam.run/documentation/externals/I would also recommend converting from a charlist to a binary string so you can perform regular Gleam string operations on the output.
If you just want to get user input, you can also use the
inputpackage which has written this FFI for you.