# Handling method calls to nullable objects

**URL:** <https://forum.crystal-lang.org/t/handling-method-calls-to-nullable-objects/2735>\
**Category:** Learning Resources\
**Created:** [December 3, 2020, 10:24am UTC](https://forum.crystal-lang.org/t/handling-method-calls-to-nullable-objects/2735 "2020-12-03T10:24:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jim2007](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jim2007/32/1042_2.png) [@jim2007](https://forum.crystal-lang.org/u/jim2007)\
**Post date:** [December 3, 2020, 10:24am UTC](https://forum.crystal-lang.org/t/handling-method-calls-to-nullable-objects/2735/1 "2020-12-03T10:24:42Z")

</div>

So I’m still on the learning curve and while I understand the error message OK, I’m unable to figure out how to make the call in a way that satisfies the compiler.

 ![](https://i.postimg.cc/Xv919bJz/crystal-1.png)

The cfg object has a property called common which is defined as nullable and common has a method called value. I thought checking if common was nil was the way to go, but obviously not.

---

<div class="post-metadata">

**Author:** ![jhass](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jhass/32/1779_2.png) [@jhass](https://forum.crystal-lang.org/u/jhass)\
**Post date:** [December 3, 2020, 10:48am UTC](https://forum.crystal-lang.org/t/handling-method-calls-to-nullable-objects/2735/2 "2020-12-03T10:48:51Z")

</div>

There’s several strategies here depending a lot on context.

If your value is indeed nilable and you should be handling the nil case on the call side, do so:

```auto
if common = cfg.common
  puts common.value("key_1")
else
  puts "common is not set"
end

```

The compiler cannot guarantee that two subsequent calls to a method return the same value, so you have to assign the return value of the call to a local variable.

If progressing with `nil` when a part of the chain is `nil` is your way to go, `try` is your friend:

```auto
puts cfg.common.try &.value("key_1")

```

You could also alter the definition of `common`, to have `common` be not nilable, and a runtime error if it is after all, and `common?` be the nilable variant. This pattern is implemented by the `getter!/property!` macros

```auto
struct Config
  getter! common = maybe_set_a_value
end

puts cfg.common.value("key_1") unless cfg.common?

```

A shortcut to implementing the “raise on nil” behavior is calling `.not_nil!`, so the above is equivalent to

```auto
struct Config
   @common = maybe_set_a_value

  def common?
    @common
  end

  def common
    @common.not_nil!
  end
end

```

---

<div class="post-metadata">

**Author:** ![jim2007](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jim2007/32/1042_2.png) [@jim2007](https://forum.crystal-lang.org/u/jim2007)\
**Post date:** [December 3, 2020, 11:05am UTC](https://forum.crystal-lang.org/t/handling-method-calls-to-nullable-objects/2735/3 "2020-12-03T11:05:06Z")

</div>

Thanks for taking the time. Very informative. I’ve not seen the:

puts cfg.common.try &.value(“key\_1”)

Before.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [December 3, 2020, 3:59pm UTC](https://forum.crystal-lang.org/t/handling-method-calls-to-nullable-objects/2735/4 "2020-12-03T15:59:31Z")

</div>

There is also a page about this in the docs: [https://crystal-lang.org/reference/syntax\_and\_semantics/if\_var.html](https://crystal-lang.org/reference/syntax_and_semantics/if_var.html)
