# Return type only checked if the function is called

**URL:** <https://forum.crystal-lang.org/t/return-type-only-checked-if-the-function-is-called/3386>\
**Category:** Offtopic\
**Created:** [June 15, 2021, 5:28am UTC](https://forum.crystal-lang.org/t/return-type-only-checked-if-the-function-is-called/3386 "2021-06-15T05:28:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![szabgab](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/szabgab/32/1278_2.png) [@szabgab](https://forum.crystal-lang.org/u/szabgab)\
**Post date:** [June 15, 2021, 5:28am UTC](https://forum.crystal-lang.org/t/return-type-only-checked-if-the-function-is-called/3386/1 "2021-06-15T05:28:23Z")

</div>

I was just playing with return types so I wrote this:

```auto
def is_odd(n : Int32) : Bool
  return n
end
x = is_odd(2)

```

This gave me the expected error: Error: method top-level is\_odd must return Bool but it is returning Int32

but if I comment out the call of the function

```auto
def is_odd(n : Int32) : Bool
  return n
end
#x = is_odd(2)

```

then it compiles and runs fine.

Even though the problem is in the function and not in the call.

I wonder if this behavior is intentional?

---

<div class="post-metadata">

**Author:** ![hugopl](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hugopl/32/970_2.png) [@hugopl](https://forum.crystal-lang.org/u/hugopl)\
**Post date:** [June 15, 2021, 5:44am UTC](https://forum.crystal-lang.org/t/return-type-only-checked-if-the-function-is-called/3386/2 "2021-06-15T05:44:20Z")

</div>

Only body of used methods are included in the type check, add a call to `is_odd` even without using its return value and you will see a compilation error.

TL;DR; If you don’t use the function, why the compiler would bother checking if you wrote it right?

---

<div class="post-metadata">

**Author:** ![szabgab](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/szabgab/32/1278_2.png) [@szabgab](https://forum.crystal-lang.org/u/szabgab)\
**Post date:** [June 15, 2021, 6:24am UTC](https://forum.crystal-lang.org/t/return-type-only-checked-if-the-function-is-called/3386/3 "2021-06-15T06:24:50Z")

</div>

Thanks for the reply.

---

<div class="post-metadata">

**Author:** ![drhuffman12](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/drhuffman12/32/715_2.png) [@drhuffman12](https://forum.crystal-lang.org/u/drhuffman12)\
**Post date:** [June 15, 2021, 7:02am UTC](https://forum.crystal-lang.org/t/return-type-only-checked-if-the-function-is-called/3386/4 "2021-06-15T07:02:05Z")

</div>

Theoretically, tests with good code coverage might help find wrong return types… so like if you have a test for every method… and if/when we again have a working code coverage tool. (See: [Code Coverage Shard for Crystal - #7 by drhuffman12](https://forum.crystal-lang.org/t/code-coverage-shard-for-crystal/3380/7))

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [June 15, 2021, 12:57pm UTC](https://forum.crystal-lang.org/t/return-type-only-checked-if-the-function-is-called/3386/5 "2021-06-15T12:57:15Z")

</div>

The examples have type restrictions in the parameters so the reader think all the information is there. But parameters might not have type information, or it might not be a concrete type (eg: `: Enumerable(T)` vs `: Array(T)`, or unbounded `: Array`).

So you need some call site to have concrete type for them.

In some places it could make sense to have a `typeof(is_odd(uninitialized Int32))` as a test if you want to make sure it type checks. But is better if you have actual tests 🙈

---

<div class="post-metadata">

**Author:** ![szabgab](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/szabgab/32/1278_2.png) [@szabgab](https://forum.crystal-lang.org/u/szabgab)\
**Post date:** [June 16, 2021, 3:12pm UTC](https://forum.crystal-lang.org/t/return-type-only-checked-if-the-function-is-called/3386/6 "2021-06-16T15:12:39Z")

</div>

I guess if I use the function in a test then crystal will already complain so this question won’t be relevant then.
