# 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:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alain](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/alain/32/745_2.png) [@Alain](https://forum.crystal-lang.org/u/Alain)\
**Post date:** [July 15, 2024, 4:16am UTC](https://forum.crystal-lang.org/t/cast-error/7018/1 "2024-07-15T04:16:59Z")

</div>

The following program fails to compile. What is the idiomatic way .

```auto
alias ANumber= Int32 | ::Nil
def addme(a : ANumber,b : ANumber) : ANumber
	if ((a!=nil)&&(b!=nil))
			a2=a.as(Int32)
			b2=b.as(Int32)
			a2+b2
		else
			nil
	end

end
c=addme(2,3)
p c
d=addme(2,nil)
p d

```

---

<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)

---

<div class="post-metadata">

**Author:** ![Alain](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/alain/32/745_2.png) [@Alain](https://forum.crystal-lang.org/u/Alain)\
**Post date:** [July 15, 2024, 4:47am UTC](https://forum.crystal-lang.org/t/cast-error/7018/3 "2024-07-15T04:47:34Z")

</div>

1. 

It’s still unclear why it works with  
a&&b  
and not with comparison to nil:  
((a!=nil)&&(b!=nil))

1. 

I also tried with exception and that did not worked also:

```auto
begin
  a+b
rescue
  nil
end

```

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [July 15, 2024, 6:40am UTC](https://forum.crystal-lang.org/t/cast-error/7018/4 "2024-07-15T06:40:57Z")

</div>

`a&&b` and `((a!=nil)&&(b!=nil))` are not equivalent, as @Blacksmoke16 said, `!=` is overloadable and can be return `whatever`, if you really like write code following your pattern, write like this:

```crystal
def addme(a : Int32?, b : Int32?) : Int32?
  if !a.nil? && !b.nil?
    a2 = a.as(Int32)
    b2 = b.as(Int32)
    a2 + b2
  else
    nil
  end
end

p! addme(2, nil) # => nil

```

---

<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, 1:05pm UTC](https://forum.crystal-lang.org/t/cast-error/7018/5 "2024-07-15T13:05:02Z")

</div>

> [@Alain](#):
>
> I also tried with exception and that did not worked also:

Right, it’s a _compile_ time error. `rescue` blocks can only handle runtime exceptions, not compile time errors.

> [@zw963](#):
>
> if you really like write code following your pattern, write like this:

FWIW you do not need the `.as(Int32)` the compiler is smart enough to do that for you, so can just do `a + b` in that branch as well. `.as` is mostly used when you have a value that could actually be say `Int32 | String` and you know its one or the other in a specific context the compiler can’t figure out; rather than using it to try and remove `Nil`.
