简体   繁体   English

`?!` 的运算符是什么?

[英]what's the operator for `!!`?

In the controller_helpers.ex of hexpm project.在 hexpm 项目的controller_helpers.ex中。 The logged_in? logged_in? function use !! function 使用!! . . 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:由于所讨论的编程语言是 Elixir,因此双感叹号 (:!) 具有以下含义:

It enforces a boolean value它强制执行 boolean 值

  • ! 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.非严格非 ( ! ) 的工作方式与not运算符相同,但不期望参数是 Boolean。

So if we have a variable life = 43 then !life will give false.因此,如果我们有一个变量life = 43 ,那么!life将给出 false。 And if we have life = nil then !life will give true.如果我们有life = nil ,那么!life将给出 true。 This operator just converts the given value to an inverted boolean value.此运算符只是将给定值转换为反转的 boolean 值。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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