简体   繁体   English

`~w(username full_name password)a` 是什么意思?

[英]what's meaning of `~w(username full_name password)a`?

In hexpm project, The line cast(%User{}, params, ~w(username full_name password)a) .hexpm项目中,行cast(%User{}, params, ~w(username full_name password)a) I know it may be equal to [:username, :full_name,:password], but why?我知道它可能等于 [:username, :full_name,:password],但为什么呢? What's the meaning of ~w and a ? ~wa什么意思?

  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

See the docs for ~w .请参阅~w的文档。

~w(username full_name password)a is just a shorthand for writing [:username, :full_name, :password] . ~w(username full_name password)a只是写[:username, :full_name, :password]的简写。 It may have been inspired by Perl's Quote-Like Operators .它可能受到 Perl 的Quote-Like Operators的启发。

  • The ~w sigil tells elixir that the space-separated items should be a list. ~w符号告诉 elixir 空格分隔的项目应该是一个列表。
  • the a modifier tells it that the items are atoms. a修饰符告诉它项目是原子。

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.直接写[:username, :full_name, :password]不需要太多努力,并且避免了从未见过印记的新手和不记得修饰符的老手提出的此类问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM