# What do I need to do to a struct to use it in a boolean context?

**URL:** <https://discourse.julialang.org/t/what-do-i-need-to-do-to-a-struct-to-use-it-in-a-boolean-context/54545>\
**Category:** General Usage\
**Created:** [February 3, 2021, 4:50pm UTC](https://discourse.julialang.org/t/what-do-i-need-to-do-to-a-struct-to-use-it-in-a-boolean-context/54545 "2021-02-03T16:50:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [February 3, 2021, 4:50pm UTC](https://discourse.julialang.org/t/what-do-i-need-to-do-to-a-struct-to-use-it-in-a-boolean-context/54545/1 "2021-02-03T16:50:41Z")

</div>

What do I need to do to a struct to use it in a boolean context?

```julia
julia&gt; struct Foo
       bool::Bool
       end

julia&gt; if Foo(true)
       println("How do I make this work?")
       end
ERROR: TypeError: non-boolean (Foo) used in boolean context
Stacktrace:
 [1] top-level scope

```

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1612371017488300?thread_ts=1612371017.488300&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 3, 2021, 7:19pm UTC](https://discourse.julialang.org/t/what-do-i-need-to-do-to-a-struct-to-use-it-in-a-boolean-context/54545/2 "2021-02-03T19:19:03Z")

</div>

> [@BridgeBot](#):
>
> What do I need to do to a struct to use it in a boolean context?

You can’t. Only `Bool` can be used in a boolean context.

(In principle, we could add an `AbstractBool` supertype of `Bool` and allow any `AbstractBool` in a boolean context, and/or add `istrue`/`isfalse` functions that you could overload.)

---

<div class="post-metadata">

**Author:** ![Ian\_Slagle](https://avatars.discourse-cdn.com/v4/letter/i/b5a626/32.png) [@Ian\_Slagle](https://discourse.julialang.org/u/Ian_Slagle)\
**Post date:** [February 3, 2021, 7:23pm UTC](https://discourse.julialang.org/t/what-do-i-need-to-do-to-a-struct-to-use-it-in-a-boolean-context/54545/3 "2021-02-03T19:23:45Z")

</div>

If it’s too much setup for the application, this should also work

```julia
if Foo.bool
       println("How do I make this work?")
end

```

---

<div class="post-metadata">

**Author:** ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)\
**Post date:** [February 4, 2021, 10:48am UTC](https://discourse.julialang.org/t/what-do-i-need-to-do-to-a-struct-to-use-it-in-a-boolean-context/54545/4 "2021-02-04T10:48:26Z")

</div>

I can think of other places where sub typing from `AbstractBool` (or implementing `istrue` or `isfalse` as appropriate for `if` statements) would simplify things. I think it would be a useful addition.

I’m working a large codebase that has deployed Bools in e.g. structs and has accessed those bools elsewhere in the code in if statements. However, pointers to those structs are also passed to a c library which expects the Bools to be 32 bit ints, where it only cares about the first bit. The problem is that it struct is the wrong size and I get a segfault.

Two solutions were proposed:

1. Keep the structs as they are and use `convert` at the point of the `ccall`. E.g.:

```julia
struct Foo
  bar::Bool
  baz::Bool
end

struct Foo_ccall
  Bar::Cint
  Baz::Cint
end
Base.convert(::Type{Foo_ccall}, foo::Foo) = Foo_ccall(Cint(foo.bar), Cint(foo.baz))

```

and use the `ccall` via

```julia
foo = Foo(true, false)#
output = ccall(func, ReturnType, (Ref{Foo_ccall},), foo)

```

rather than the original

```julia
output = ccall(func, ReturnType, (Ref{Foo},), foo)

```

1. Swap out the `Bool`s in the structs for `Cint`s and refactor the if statements.

I ended up doing 1) to make sure that I was solving the right problem and then 2) because the technical debt had to be paid at some point.
