# Cast error

**URL:** <https://forum.crystal-lang.org/t/cast-error/7018>\
**Category:** Help & Support\
**Created:** [July 15, 2024, 4:16am UTC](https://forum.crystal-lang.org/t/cast-error/7018 "2024-07-15T04:16:59Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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 15, 2024, 4:21am UTC](https://forum.crystal-lang.org/t/cast-error/7018/2 "2024-07-15T04:21:04Z")

</div>

The idiomatic way would be like:

```crystal
def addme(a : Int32?, b : Int32?) : Int32?
  return unless a && b
  
  a + b
end

```

To handle `nil` you need to either use the special `.nil?` method, `.try`, or just check the variables directly as `nil` is a falsely value so the compiler can know they won’t be `nil`. `!=` does not work because it’s possible to override it to return whatever, so the compiler cannot trust it.

Another way would be to have two overloads of the method, one only accepting `Int32` and the other accepting `Int32?` with the former just being `a + b` and the latter `nil` return values.

EDIT: Maybe also checkout [[Screencast] Handling Nil in Crystal](https://forum.crystal-lang.org/t/screencast-handling-nil-in-crystal/4428)

---

_[View the full topic](https://forum.crystal-lang.org/t/cast-error/7018)._
