# Compiling error for Nilable var due to undefined method

**URL:** <https://forum.crystal-lang.org/t/compiling-error-for-nilable-var-due-to-undefined-method/4193>\
**Category:** Help & Support\
**Created:** [December 27, 2021, 1:19pm UTC](https://forum.crystal-lang.org/t/compiling-error-for-nilable-var-due-to-undefined-method/4193 "2021-12-27T13:19:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pan](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/pan/32/1475_2.png) [@Pan](https://forum.crystal-lang.org/u/Pan)\
**Post date:** [December 27, 2021, 1:19pm UTC](https://forum.crystal-lang.org/t/compiling-error-for-nilable-var-due-to-undefined-method/4193/1 "2021-12-27T13:19:08Z")

</div>

Below code gets an `Error: undefined method '<<' for Nil (compile-time type is (Array(Node) | Nil))`. The `@children` is initialized on demand when it is nil in method `add_child`, so the code should work. I expect the `@children` should be nil when it is a leaf node by default. But it has the compiling error, how to fix this?

```crystal
class Node
  getter children : Array(self)? = nil

  def initialize(@name : String)
  end

  def add_child(child : self)
    @children = Array(self).new if children.nil?
    @children << child
  end
end

Node.new("root").add_child(Node.new("a child"))
```

---

<div class="post-metadata">

**Author:** ![mdwagner](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/mdwagner/32/1077_2.png) [@mdwagner](https://forum.crystal-lang.org/u/mdwagner)\
**Post date:** [December 27, 2021, 1:58pm UTC](https://forum.crystal-lang.org/t/compiling-error-for-nilable-var-due-to-undefined-method/4193/2 "2021-12-27T13:58:41Z")

</div>

You’re gonna need to chain `.not_nil!` on all calls to `children`, because like you said it gets initialized sometime after creating the class instance. You could also look at `getter!`, as that provides a method that does the non\_nil check for you and use that.

---

<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 27, 2021, 2:32pm UTC](https://forum.crystal-lang.org/t/compiling-error-for-nilable-var-due-to-undefined-method/4193/3 "2021-12-27T14:32:26Z")

</div>

`.not_nil!` is really bad advice. You should avoid that. It’s only meant as a last resort when you can’t write code in a way that the compiler can safely exclude `Nil`. This is not a use case for `.not_nil!` because it’s trivial to fix the code for proper type propagation.

Instead, you should assign the instance variable to a local variable and do the nil check on that. That enables the compiler to properly track the types without any explicit nil checking.

```auto
children = @children ||= Array(self).new
children << child

```

The language reference explains the reasons for this.  
[https://crystal-lang.org/reference/1.2/syntax\_and\_semantics/if\_var.html#limitations](https://crystal-lang.org/reference/1.2/syntax_and_semantics/if_var.html#limitations)

---

<div class="post-metadata">

**Author:** ![mdwagner](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/mdwagner/32/1077_2.png) [@mdwagner](https://forum.crystal-lang.org/u/mdwagner)\
**Post date:** [December 27, 2021, 2:38pm UTC](https://forum.crystal-lang.org/t/compiling-error-for-nilable-var-due-to-undefined-method/4193/4 "2021-12-27T14:38:43Z")

</div>

I didn’t know it was bad advice, but depending on the context, I can see how it can cause some issues. But to your point, it might not be the most preferred way and can be avoided.

---

<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:** [December 27, 2021, 3:16pm UTC](https://forum.crystal-lang.org/t/compiling-error-for-nilable-var-due-to-undefined-method/4193/5 "2021-12-27T15:16:27Z")

</div>

I like to use the block variant of the `getter` macro for these cases:

```auto
getter children : Array(self) { Array(self).new }

# ...

self.children << child

```

Essentially does the `||=` logic for you under the hood. Granted the one difference is that if you call `.children` outside of the class you could get an empty array instead of `nil`. If that’s okay then 👍.
