【问题标题】:What does the Circumflex sign/Pin/Cap operator (^) do in Elixir?Elixir 中的 Circumflex 符号/Pin/Cap 运算符 (^) 有什么作用?
【发布时间】:2015-09-05 16:07:37
【问题描述】:

当我进入插值部分时,我正在浏览 Ecto 文档,其中 Ecto 使用 Circumflex(^) 符号,就像这样。

def with_minimum(age, height_ft) do
    from u in User,
  where: u.age > ^age and u.height > ^(height_ft * 3.28)
end

让我想知道,它有什么作用? :-)

【问题讨论】:

    标签: elixir


    【解决方案1】:

    在 Elixir 中,pin 运算符用于模式匹配以匹配变量的当前值。你可以在这里阅读更多信息:http://elixir-lang.org/getting-started/pattern-matching.html

    Ecto 将 pin 运算符更改为查询插值,您将 Elixir 值传递给查询。您可能会争辩说它们的行为有些相似,因为数据库正在有效地运行查询以尝试找到匹配的值,但最简单的方法是将其视为查询插值。更多信息在这里:http://hexdocs.pm/ecto/Ecto.Query.html

    【讨论】:

      猜你喜欢
      • 2017-11-24
      • 2011-01-27
      • 2010-09-14
      • 2011-01-24
      • 2010-09-08
      相关资源
      最近更新 更多