top | item 10354400

Thinking About Function Signatures in Elixir

40 points| jaxondu | 10 years ago |rob.conery.io

5 comments

order
[+] eridius|10 years ago|reply
That looks neat. But it also looks to me like your error variant has the wrong arity.

  def charge_customer({:error, err}), do: {:error, err}
  def charge_customer({:ok, cart}, card) do
The first one takes a single argument {:error, err}, but the second one takes two arguments, {:ok, cart} and card.

Same issue with the keyword list version, your error function doesn't take a keyword list at all, just the {:error, err} argument.

[+] robconery|10 years ago|reply
The first function matches any call to it with an error - this is how (apparently) you pass error information on to the result. There's no graceful exit with an error.

The keyword list is pointless with an error function.

[+] vezzy-fnord|10 years ago|reply
This is, of course, exactly identical to how things work in Erlang, as well. A lot of conditional logic is done implicitly through polymorphic functions of the same arity that pattern match on parameters. Proplists are a surprisingly versatile data structure to exploit.
[+] vvanders|10 years ago|reply
Man, cases like this just make me appreciate pattern matching in function signatures even more.