Pattern matching a map as a parameter of a function

I have the following test code to test pattern matching on functions sending a map as a parameter

defmodule TestMapAsParam do
  def resolve(
        %{"timezone" => timezone, "store_number" => store_number}
      )
      when is_binary(store_number) do
    IO.puts "This is the #{timezone} and this is the #{store_number}"
  end
end

timezone = "eastern"
store_number = 50001
TestMapAsParam.resolve(%{"timezone" => timezone, "store_number" => store_number})```

By testing this code running the elixir command in the terminal it throws the following error:

** (FunctionClauseError) no function clause matching in TestMapAsParam.resolve/1

The following arguments were given to TestMapAsParam.resolve/1:

    # 1
    %{"store_number" => 50001, "timezone" => "eastern"}

TEST_MAP_AS_PARAM.ex:2: TestMapAsParam.resolve/1
(elixir 1.13.1) lib/code.ex:1183: Code.require_file/2
As far as I understand the paramters I sent to the function doesn't match the ones on the declaration I did on the module?, can someeone help me to figure out how can I make it match? thanks for the help.

OS: MACos Venture 13.0.1
Elixir version: 1.13.1  with Erlang/OTP 24)

I think the map matches but you have a guard test which requires store_number to be a binary when it is 50001 which is a number.

3 Likes

Thanks @rvirding It works now! changed it to a string so it matches the is_binary function guard.
thanks a lot for your help feeling silly now.

defmodule TestMapAsParam do
  def resolve(%{"timezone" => timezone, "store_number" => store_number})
      when is_binary(store_number) do
    IO.puts "This is the #{timezone} and this is the #{store_number}"
  end
end

timezone = "eastern"
store_number = "50001"
TestMapAsParam.resolve(%{"timezone" => timezone, "store_number" => store_number})

We all make mistakes, don’t worry about that :slightly_smiling_face:

1 Like

The simple mistakes are often difficult to find just because they are simple. It is like going around at home trying to find something and then you see you have been holding it all the time. :smiley:

2 Likes