# How to Bool

**URL:** <https://forum.crystal-lang.org/t/how-to-bool/5869>\
**Category:** Help & Support\
**Created:** [July 23, 2023, 6:12pm UTC](https://forum.crystal-lang.org/t/how-to-bool/5869 "2023-07-23T18:12:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xen](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/xen/32/2288_2.png) [@Xen](https://forum.crystal-lang.org/u/Xen)\
**Post date:** [July 23, 2023, 6:12pm UTC](https://forum.crystal-lang.org/t/how-to-bool/5869/1 "2023-07-23T18:12:05Z")

</div>

So far I’ve been content with using as true and nil as false in conditionals.

But now I want a Bool var but discover that (assume `x` and `y` are `String?`):

```auto
z : Bool = (x && y).as(Bool)

```

Gives me “Error: can’t cast (String | Nil) to Bool”.

Of course I can:

```auto
z : Bool = x && y ? true : false

```

But that feels very clunky. Aren’t there a better way?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [July 23, 2023, 6:24pm UTC](https://forum.crystal-lang.org/t/how-to-bool/5869/2 "2023-07-23T18:24:45Z")

</div>

As per [&& - Crystal](https://crystal-lang.org/reference/1.9/syntax_and_semantics/and.html), `z = x && y` is syntactically the same as:

```crystal
tmp = x
z = if tmp
    y
  else
    tmp
  end

```

In order to actually turn two `String?` into a `Bool`, you’d prob want to do like `z = !x.nil? && !y.nil?` (or maybe even [#presence](https://crystal-lang.org/api/String.html#presence%3Aself%7CNil-instance-method)). Could also shorten that up to `z = !!(x && y)` as well, but at the cost of readability/not handling empty strings if that’s important.

Also worth pointing out, if you’re using this as part of an `if` statement, then you can just do `if x && y` due to [Truthy and falsey values - Crystal](https://crystal-lang.org/reference/1.9/syntax_and_semantics/truthy_and_falsey_values.html), only really need a direct `Bool` if you want to pass that variable around/reuse it.

---

<div class="post-metadata">

**Author:** ![Xen](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/xen/32/2288_2.png) [@Xen](https://forum.crystal-lang.org/u/Xen)\
**Post date:** [July 23, 2023, 6:40pm UTC](https://forum.crystal-lang.org/t/how-to-bool/5869/3 "2023-07-23T18:40:25Z")

</div>

Oh yeah, I know of the joys of truthy/falsy values. It’s refreshingly simple compared to PHP/JS.

But I’d hoped that double negation would stick to roaming the JS world.

I was hoping for something simple ala `(Object | Nil)#to_b` (or `(Object | Nil)#thruthy?` would make sense).

Bool is probably a bit overlooked due to the fact that one doesn’t really need it 99% of the time…

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [July 23, 2023, 6:49pm UTC](https://forum.crystal-lang.org/t/how-to-bool/5869/4 "2023-07-23T18:49:11Z")

</div>

Related: [Add a .to\_bool method? · Issue #7538 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/7538)

---

<div class="post-metadata">

**Author:** ![Xen](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/xen/32/2288_2.png) [@Xen](https://forum.crystal-lang.org/u/Xen)\
**Post date:** [July 23, 2023, 7:04pm UTC](https://forum.crystal-lang.org/t/how-to-bool/5869/5 "2023-07-23T19:04:29Z")

</div>

_grunt_ Well that’s that, guess I’ll have to get used to `!!` then.

I do think the argument is a bit weak, though. If having `#truthy?` as an alternative to `!!` is considered superfluous, why do we have `#nil?`? `x == nil` should evaluate the same.

_shrugs_

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [July 23, 2023, 7:16pm UTC](https://forum.crystal-lang.org/t/how-to-bool/5869/6 "2023-07-23T19:16:35Z")

</div>

> [@Xen](#):
>
> why do we have `#nil?`? `x == nil` should evaluate the same.

[https://play.crystal-lang.org/#/r/fi89](https://play.crystal-lang.org/#/r/fi89)

```crystal
v = 1 || nil
 
pp typeof(v) # => (Int32 | Nil)
 
unless v == nil
  pp typeof(v) # => (Int32 | Nil)
end
 
unless v.nil?
  pp typeof(v) # => Int32
end

```

I think the key difference in this context is `.nil?` is a pseudo compiler method which allows the compiler to change/guarantee the type of a variable. Whereas `#==` is a simple method that can be overridden to do something like `nil == nil # => false` or `nil != nil # => true`, hence why in this example the type of `v` is still `Int32 | Nil`.
