This is more of a general question, but I’m wondering how other people in the community think about the pattern matching in function signatures.
Pattern matching is fairly straight-forward when you match on simple values, e.g.
def something([]), do: "Emtpy!"
I start to get some metal friction when I look at type-hinting when there are functions like this:
def something(c = %Plug.Conn{}), do: "Something with the conn"
At first glance, I would think that c
contains the empty struct, but of course, it will have the FULL value of whatever was passed to the function so long as the input was of the proper type. In other words, it’s not really a pattern-match at all, it’s a type hint.
Granted, my confusion here is probably the baggage of seeing that syntax used not for type-hinting, but for supplying a default value in many other languages (e.g. PHP, Ruby, Python).
The pattern matching/type-hinting gets a bit stranger for me when it gets nested inside tuples. Consider the following example:
my_tuple = MyContext.get_resource_as_tuple()
case my_tuple do
{:ok, resource = %{status_id: "valid"}} -> result
{:ok, %{status: status}} -> "Boo. Status #{status} is not valid."
{:error, msg} -> "Error: #{msg}"
end
Again, the resource = %{status_id: "valid"}
looks more like an assignment, and I have to remind myself how it actually works. Go, for example, omits the equals sign and puts the type after the variable when it is used as part of a type-check. PHP, puts the variable type in front of the variable when it’s used as part of a type-check.
How do others think about this when they’re walking through code?