Default argument which is the 1st one and not the only one. Why does this work?

In a third-party project, in the models I've encountered functions like this:

  def changeset(model \\ %__MODULE__{}, params) do
      |> cast(params, @required_fields ++ @optional_fields)
      |> validate_required(@required_fields)

How can a function have a default argument which is both a) first and b) followed by a mandatory one?

What's interesting, these function work properly.


  • Default arguments in elixir are just syntactic sugar, after compilation elixir will generate these functions for you:

    changeset(model, params)

    This is different from how this works in other languages and I would not encourage abusing it, as it tends to make code unreadable, especially if you have multiple default arguments.