#Protocol implementation for an existing record.

15 messages · Page 1 of 1 (latest)

hollow cove
#

I am trying to implement an Elixir protocol for records of an underlying Erlang library uses.
(Records are tuples with the first element being an atom identifying the record type.)

Is that possible, if yes how?

green cliff
hollow cove
#

I sure have looked at it, but I haven't seen a way to achieve what I hope for.

green cliff
#

i thought extract with :from_lib might be what you want

hollow cove
#

You can implement protocols for Tuples, but you can only do it for all tuples or none of them, right?
You can't pattern match against a specific group of tuples and only implement a protocol for them, right?

hollow cove
green cliff
#

put all the record types into a module attribute, and use that in your protocol to see if you want to mess with it? sounds like it could produce unintended consequences though

leaden ore
#

You cannot implement protocols for records

#

If you want to do so, then you need to create wrapper struct

green cliff
leaden ore
#

You can implement it for tuples, but all tuples, without distinction.

green cliff
#

yeah so could you not load all the record types at compile time and then if it matches {User, _, _, _} {Project, _, _, _, _, _} for example. do some manipulation, otherwise just pass it through as is

#

feels like it should work but i dont have the time/effort in me to try it out

hollow cove
#

I packed the record in a struct