Hey everyone,
I’m currently learning Elixir and I’m trying to understand the difference between two examples I came across. Here are the examples in question:
Example 1:
def create_video(%Accounts.User{} = user, attrs \\ %{}) do %Video{} |> Video.changeset(attrs) |> Ecto.Changeset.put_assoc(:user, user) |> Repo.insert() end
Example 2:
def create_video(user = %Accounts.User{}, attrs \\ %{}) do %Video{} |> Video.changeset(attrs) |> Ecto.Changeset.put_assoc(:user, user) |> Repo.insert() end
I know that outside of the function signature, pattern matching and variable binding have different meanings. But I’m not sure if there’s any functional difference between these two examples in terms of the code that’s executed. My understanding is that create_videowill only be called if a struct of type Accounts.Useris passed as an argument in both examples.
I’m currently reading the “Programming Phoenix” book, where I came across Example 1. However, I also saw Example 2 in the “Programming Elixir” book by Pragmatic Programmers. Can anyone help me understand the practical differences between these two examples? Is there a preference which style to use in this context?
Thanks in advance for your help!