3
submitted 10 months ago by salvador@lemmy.world to c/elixir@programming.dev

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

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

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

What's interesting, they work properly.

top 1 comments
sorted by: hot top controversial new old
[-] stifle867@programming.dev 1 points 9 months ago

That's interesting. What if you were to call it with only 1 argument? Would that not match, or would it match as the 1st argument is using the default?

this post was submitted on 21 Oct 2023
3 points (71.4% liked)

Elixir

467 readers
13 users here now

founded 1 year ago
MODERATORS