# Compiler error assigning \`nil\` to instance var

**URL:** <https://forum.crystal-lang.org/t/compiler-error-assigning-nil-to-instance-var/5409>\
**Category:** Help & Support\
**Created:** [February 21, 2023, 2:16pm UTC](https://forum.crystal-lang.org/t/compiler-error-assigning-nil-to-instance-var/5409 "2023-02-21T14:16:34Z")\
**Posts on this page:** 6\
**Page:** 1

<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:** [February 21, 2023, 2:16pm UTC](https://forum.crystal-lang.org/t/compiler-error-assigning-nil-to-instance-var/5409/1 "2023-02-21T14:16:34Z")

</div>

> [@\[Discuss\] Formatter always format one-line method to multi-line, is it necessary?](https://forum.crystal-lang.org/t/discuss-formatter-always-format-one-line-method-to-multi-line-is-it-necessary/5407/6):
>
> If we allow the two forms then people will start making comments like “Hey, you should make this a one-liner” and so on. By not having the option to choose everything is simplified.

Hi, I am thinking of another question.

Following code not work

```crystal
class A
  def initialize
    @x : Int32? = nil
  end
end

a = A.new

# 3 | @x : Int32? = nil
# ^
# Error: instance variable @x of A was inferred to be Nil, but Nil alone provides no information

```

But following code is working.

```crystal
class A
 @x : Int32?

 def initialize
   @x = nil
 end
end

```

So, this is also because `not having the option to choose everything is simplified`, right?

---

<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:** [February 21, 2023, 2:20pm UTC](https://forum.crystal-lang.org/t/compiler-error-assigning-nil-to-instance-var/5409/2 "2023-02-21T14:20:12Z")

</div>

I think it’s because you’re typing the ivar within a method. If you remove the `= nil` you get the actual problem that:

> Error: declaring the type of an instance variable must be done at the class level

The error you’re getting is because the ivar isn’t typed at the class level, its assumed to be nilable, but since you’re assigning `nil` the type it infers is `Nil` which probably isn’t what you want.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [February 21, 2023, 2:57pm UTC](https://forum.crystal-lang.org/t/compiler-error-assigning-nil-to-instance-var/5409/3 "2023-02-21T14:57:43Z")

</div>

I think this is a bug. Please report it. Thank you!

---

<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:** [February 21, 2023, 3:10pm UTC](https://forum.crystal-lang.org/t/compiler-error-assigning-nil-to-instance-var/5409/4 "2023-02-21T15:10:10Z")

</div>

Sorry, how to report it? what is the expected behavior for the former code? 1 or 2 ?

1. shouldn’t raise error?

2. should raise error, but with a more non-confused message?

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [February 21, 2023, 4:12pm UTC](https://forum.crystal-lang.org/t/compiler-error-assigning-nil-to-instance-var/5409/5 "2023-02-21T16:12:17Z")

</div>

I think it should just work.

---

<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:** [February 21, 2023, 4:32pm UTC](https://forum.crystal-lang.org/t/compiler-error-assigning-nil-to-instance-var/5409/6 "2023-02-21T16:32:25Z")

</div>

> <https://github.com/crystal-lang/crystal/issues/13101>
>
> \## Bug Report
> 
> Following code should just work instead of raise error.
> 
> \`\`\`c…rystal
> class A
> def initialize
> @x : Int32? = nil
> end
> end
> 
> a = A.new
> 
> \# 3 | @x : Int32? = nil
> \# ^
> \# Error: instance variable @x of A was inferred to be Nil, but Nil alone provides no information
> \`\`\`
> 
> More details, check discuss https://forum.crystal-lang.org/t/compiler-error-assigning-nil-to-instance-var/5409
