简体   繁体   中英

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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