I am trying to write tests in Elixir that call Erlang code similar to this:
foo([{property_one = P1, property_two = P2}|_] = Rows) when P1 =/= P2 ->
erlang:display("function head one"),
foo(Rows) ->
erlang:display("function head two"),
Rows.
I think that an Elixir keyword list would be the correct data type to pass to this function, but I cannot seem to construct it properly. This is the code I'm trying to call it with:
:module.foo([[property_one: "A", property_two: "B"]])
But this code goes direct to function head two. Where am I going wrong?
Update: Going to git history for the file revealed that a record declaration had been dropped somewhere along the way. Modifying the code to:
foo([#record{property_one = P1, property_two = P2}|_] = Rows)
fixed all problems
foo
function? That first clause only matches a list starting with the tuple{property_one, property_two}
. This should match it::module.foo([{:property_one, :property_two}])
.P1
will always be:property_one
andP2
will always be:property_two
with the current Erlang code. – Dogbert{
in the pattern? Records in Erlang are stored as plain tuples without property key names. To create Erlang records in Elixir, you should use theRecord
module as explained here: hexdocs.pm/elixir/Record.html. – Dogbert