I’m doing different implementations in Erlang and one of them let me encapsulate in a record all of the data. Obviously, that’s converted finally to a Tuple. The problem I had is if I try to get that from Erlang to Elixir, I could to create a module like this:
defmodule EphpArray do
@ephp_hrl "ephp/include/ephp.hrl"
import Record, only: [defrecord: 2]
defrecord :ephp_array, Record.extract(:ephp_array, from_lib: @ephp_hrl)
end
But I cannot implement effectively a Collectable
to handle Enum.into/1
in a correct way, what’s missing? Is it possible?
You can’t define protocol implementations for records. However, you could probably get it to work by making it a struct as well.
defmodule EphpArray do
@ephp_hrl "ephp/include/ephp.hrl"
import Record, only: [defrecord: 2]
defrecord :ephp_array, Record.extract(:ephp_array, from_lib: @ephp_hrl)
# for protocols
defstruct []
def new, do: %__MODULE__{}
end
defimpl Collectable, for: EphpArray do
def into(original) do
# implementation
end
end
Enum.into([1, 2, 3], EphpArray.new)
1 Like
I was trying with Tuple
and it’s working, but of course, that’s capturing all of the tuples and not only which are related to this record.
I’ll check to transform the record to map just to interact in Elixir in this way, I think it could be easier than with records or tuples. Thanks for the tip @blatyo !