Home > Back-end >  what's meaning of `~w(username full_name password)a`?
what's meaning of `~w(username full_name password)a`?

Time:10-18

In hexpm project, The line cast(%User{}, params, ~w(username full_name password)a). I know it may be equal to [:username, :full_name,:password], but why? What's the meaning of ~w and a?

  def build(params, confirmed? \\ not Application.get_env(:hexpm, :user_confirm)) do
    cast(%User{}, params, ~w(username full_name password)a)
    |> validate_required(~w(username password)a)
    |> cast_assoc(:emails, required: true, with: &Email.changeset(&1, :first, &2, confirmed?))
    |> cast_embed(:tfa)
    |> update_change(:username, &String.downcase/1)
    |> validate_length(:username, min: 3)
    |> validate_format(:username, @username_regex)
    |> validate_format(:username, @username_reject_regex)
    |> validate_exclusion(:username, @reserved_names)
    |> unique_constraint(:username, name: "users_username_idx")
    |> validate_length(:password, min: 7)
    |> validate_confirmation(:password, message: "does not match password")
    |> update_change(:password, &Auth.gen_password/1)
  end

CodePudding user response:

See the docs for ~w.

~w(username full_name password)a is just a shorthand for writing [:username, :full_name, :password]. It may have been inspired by Perl's Quote-Like Operators.

  • The ~w sigil tells elixir that the space-separated items should be a list.
  • the a modifier tells it that the items are atoms.

Considering code is read much more often than written, I personally do not use this sigil. Writing [:username, :full_name, :password] directly does not take much effort and avoids questions like this from both newbies that have never seen the sigil, and for old hands that don't remember the modifiers.

  • Related