Why use `Ecto.Changeset.get_change/3` instead of just accessing `changeset.changes` directly?

So Ecto has the Ecto.Changeset.get_change/3 function, and I’m honestly not sure what purpose it serves.

Here is the current code for this function:

  @spec get_change(t, atom, term) :: term
  def get_change(%Changeset{changes: changes} = _changeset, key, default \\ nil)
      when is_atom(key) do
    Map.get(changes, key, default)
  end

I’m wondering if there’s really a reason to use it at all if it’s basically just a wrapper for Map.get/3?

Am I missing something? Is there any disadvantage to just using changeset.changes directly?

Thanks

You get (runtime) type-checking by using get_change which isn’t nothing. :changes is considered public, though, so there is theoretically no harm in accessing it directly.

1 Like