# How to call the "logical and" operator as a function?

**URL:** <https://discourse.julialang.org/t/how-to-call-the-logical-and-operator-as-a-function/104094>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 21, 2023, 6:30am UTC](https://discourse.julialang.org/t/how-to-call-the-logical-and-operator-as-a-function/104094 "2023-09-21T06:30:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)\
**Post date:** [September 21, 2023, 6:30am UTC](https://discourse.julialang.org/t/how-to-call-the-logical-and-operator-as-a-function/104094/1 "2023-09-21T06:30:32Z")

</div>

We can call the add operator as `Base.:+`. Can I do the same to logical and?

```julia
julia> Base.:+(3, 4)
7

julia> Base.:&&(true, false)
ERROR: UndefVarError: && not defined
Stacktrace:
 [1] getproperty(x::Module, f::Symbol)
   @ Base .\Base.jl:31
 [2] top-level scope
   @ REPL[7]:1

julia> true && false
false

```

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [September 21, 2023, 6:34am UTC](https://discourse.julialang.org/t/how-to-call-the-logical-and-operator-as-a-function/104094/2 "2023-09-21T06:34:21Z")

</div>

I don’t think so.  
Because `&&` is actually control flow, like `if` or `?` not simply a function like `+` is.  
Due to having short-circuiting behavior

---

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [September 21, 2023, 6:35am UTC](https://discourse.julialang.org/t/how-to-call-the-logical-and-operator-as-a-function/104094/3 "2023-09-21T06:35:34Z")

</div>

You can use the function `all`, for instance `all([true,false])`.

---

<div class="post-metadata">

**Author:** ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)\
**Post date:** [September 21, 2023, 6:40am UTC](https://discourse.julialang.org/t/how-to-call-the-logical-and-operator-as-a-function/104094/4 "2023-09-21T06:40:22Z")

</div>

Thanks. I actually want the Boolean operators: and, or, not.

Interestingly, I just realized that the bitwise operations can work here.

```julia
julia> Base.:&(1, 0)
0

julia> Base.:&(1, 1)
1

julia> Base.:&(true, false)
false

julia> Base.:&(true, true)
true

julia> Base.:~(true)
false

julia> Base.:|(true, true)
true

julia> Base.:|(true, false)
true

```

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [September 21, 2023, 7:08am UTC](https://discourse.julialang.org/t/how-to-call-the-logical-and-operator-as-a-function/104094/5 "2023-09-21T07:08:14Z")

</div>

ah yes, the when applied to `Bool`s bitwise `&` is exactly equal to “logical and”

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [September 21, 2023, 7:44pm UTC](https://discourse.julialang.org/t/how-to-call-the-logical-and-operator-as-a-function/104094/6 "2023-09-21T19:44:29Z")

</div>

As has been vaguely hinted, the issue here is that `&&` and `||` are actually control flow constructs (like `if`) rather than functions (like `+`). But yes, some combination of `&`, `|`, `all`, and `any` is what you’re after.

Of course, you can always make your own functions if you need. For example, `(x,y) -> x && y` would have worked. But you should obviously just use `Base.:&` here, and new functions will not ~~allow you to extend do not change the behavior of `Base.:&` to new types~~ change the behavior of `Base.:&` (edited for clarity).

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 21, 2023, 7:56pm UTC](https://discourse.julialang.org/t/how-to-call-the-logical-and-operator-as-a-function/104094/7 "2023-09-21T19:56:42Z")

</div>

> [@mikmoore](#):
>
> But you should obviously just use `Base.:&` here, and new functions will not allow you to extend `Base.:&` to new types.

Not sure I follow - you’re very much able to define methods on new types for `&`. It’s a function like any other.
