Chen Yu
Chen Yu

Reputation: 4077

what's the operator for `!!`?

In the controller_helpers.ex of hexpm project. The logged_in? function use !!. What's the meaning of this operation?

  def logged_in?(conn) do
    !!conn.assigns[:current_user]
  end

Upvotes: 0

Views: 182

Answers (2)

Muhammad Usama
Muhammad Usama

Reputation: 180

First we take a look at ! operator

Non-strict not (!) works same as not operator but does not expect the argument to be a Boolean.

So if we have a variable life = 43 then !life will give false. And if we have life = nil then !life will give true. This operator just converts the given value to an inverted boolean value.

And now !!

Actually !! is not an operator it's just that the ! operator is used twice. By adding another ! we are just inverting the result of the first ! operator.

life = 42
    
!life  // Inverted Boolean (false)
!!life // Non-inverted Boolean (true)

end = nil

!end  // Inverted Boolean (true)
!!end // Non-inverted Boolean (false)

Upvotes: 1

tomerpacific
tomerpacific

Reputation: 6475

Since the programming language in question is Elixir, the double exclamation mark (!!) has the following meaning:

It enforces a boolean value

  • ! means not

  • !! means not not forcing true or false

It is a double application of a unary operator.

Documentation

Upvotes: 3

Related Questions