Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the Circumflex sign/Pin/Cap operator (^) do in Elixir?

Tags:

elixir

I was browsing through the Ecto documentation, when i got to the interpolation part, where Ecto uses the Circumflex(^) sign, like so.

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

Made me wonder, what does it do? :-)

like image 508
MartinElvar Avatar asked Sep 04 '15 19:09

MartinElvar


1 Answers

In Elixir, the pin operator is used in pattern matching to match on the current value of a variable. You can read more about it here: http://elixir-lang.org/getting-started/pattern-matching.html

Ecto changes the pin operator to mean query interpolation where you pass an Elixir value to the query. You could argue their behaviour are somewhat similar, because the database is effectively running the query trying to find a value that matches, but the easiest way is to think of it is indeed as query interpolation. More info here: http://hexdocs.pm/ecto/Ecto.Query.html

like image 70
José Valim Avatar answered Oct 21 '22 13:10

José Valim