Hey all, I have a set of pipelines that need to perform operations on some list. These operations should be implemented differently depending on the type of the list, but the pipeline itself is not concerned with how they are implemented. I was thinking of using a protocol for this and creating two custom list types, one called MinList
and other MaxList
. Then, the pipeline can just use the protocol function and everything will behave as expected.
However, I am unable to find documentation on how to define a custom list type. I tried doing this:
defprotocol SpecialList do
@spec empty?(t) :: boolean
def empty?(t)
@spec update(t, elem) :: t
def update(t, elem)
end
defmodule MinList do
@type t() :: list(number())
end
defimpl SpecialList, for: FinalMix.MinList do
def empty?(list) do
... # Implement
end
def update(list, elem) do
... # Implement
end
end
However, when I try iex and do
iex> MinList |> SpecialList.empty
I get errors saying the protocol is not implemented for type atom. Any tips here?