What do the two exclamations points in this code mean?

Hello,

I saw this code in the Bodyguard package. And I’m sorry if this is newbie question, but what is the purpose of the ‘!!’ operator before the apply function in this code. I’ve tried to search for answers here and in the docs, but I must have missed it.

Thanks all

def authorized?(user, action, term, opts \ []) do

module = opts[:policy] || policy_module(term)
!!apply(module, :can?, [user, action, term])

end

2 Likes

! negates the expression:

iex> a = true
true
iex> !a
false
iex> a = 1
1
iex> !a
false
iex> a = nil
nil
iex> !a
true

!! negates the negated expression and so it turns a truthy value into a boolean:

iex> a = true
true
iex> !!a
true # Not very useful because we’ve ended up with the same value.
iex> a = 1
1
iex> !!a
true
iex> a = nil
nil
iex> !!a
false

Docs: http://elixir-lang.org/docs/stable/elixir/Kernel.html#!/1

4 Likes

I knew it would be obvious. Thanks

1 Like