# Can't infer the type of instance variable in class

**URL:** <https://forum.crystal-lang.org/t/cant-infer-the-type-of-instance-variable-in-class/1181>\
**Category:** Help & Support\
**Created:** [September 30, 2019, 11:18am UTC](https://forum.crystal-lang.org/t/cant-infer-the-type-of-instance-variable-in-class/1181 "2019-09-30T11:18:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![orangeSi](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/orangesi/32/1569_2.png) [@orangeSi](https://forum.crystal-lang.org/u/orangeSi)\
**Post date:** [September 30, 2019, 11:18am UTC](https://forum.crystal-lang.org/t/cant-infer-the-type-of-instance-variable-in-class/1181/1 "2019-09-30T11:18:09Z")

</div>

hello, everyone! My code is like this:

```auto
class Hello
  def initialize()
  end
  def go(name : Int32|String = 32)
	name = "" if name.is_a?(Int32)
	@name = name
  end
end

p = Hello.new
p.go()

```

then build

```auto
$ crystal build tmp6.cr 

```

got this weird error:

```auto
Showing last frame. Use --error-trace for full trace.

In tmp6.cr:7:2

 7 | @name = name
     ^----
Error: can't infer the type of instance variable '@name' of Hello

The type of a instance variable, if not declared explicitly with
`@name : Type`, is inferred from assignments to it across
the whole program.
he assignments must look like this:

  1. `@name = 1` (or other literals), inferred to the literal's type
  2. `@name = Type.new`, type is inferred to be Type
  3. `@name = Type.method`, where `method` has a return type
     annotation, type is inferred from it
  4. `@name = arg`, with 'arg' being a method argument with a
     type restriction 'Type', type is inferred to be Type
  5. `@name = arg`, with 'arg' being a method argument with a
     default value, type is inferred using rules 1, 2 and 3 from it
  6. `@name = uninitialized Type`, type is inferred to be Type
  7. `@name = LibSome.func`, and `LibSome` is a `lib`, type
     is inferred from that fun.
  8. `LibSome.func(out @name)`, and `LibSome` is a `lib`, type
     is inferred from that fun argument.

Other assignments have no effect on its type.

can't infer the type of instance variable '@name' of Hello

```

but I think `@name` should be `String`, is it right?

thanks~

---

<div class="post-metadata">

**Author:** ![Exilor](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/exilor/32/780_2.png) [@Exilor](https://forum.crystal-lang.org/u/Exilor)\
**Post date:** [September 30, 2019, 12:08pm UTC](https://forum.crystal-lang.org/t/cant-infer-the-type-of-instance-variable-in-class/1181/2 "2019-09-30T12:08:57Z")

</div>

> [@orangeSi](#):
>
> name = “” if name.is\_a?(Int32)

That’s the line that prevents your code from working. Perhaps you should provide a default value for `@name`:

```auto
class Hello
  def initialize
    @name = "" # or outside #initialize
  end

  def go(name : Int32 | String = 32)
    name = "" if name.is_a?(Int32)
    @name = name
  end
end

```

That way the compiler knows what type `@name` is supposed to be from the get-go.

---

<div class="post-metadata">

**Author:** ![girng\_github](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/girng_github/32/571_2.png) [@girng\_github](https://forum.crystal-lang.org/u/girng_github)\
**Post date:** [September 30, 2019, 1:55pm UTC](https://forum.crystal-lang.org/t/cant-infer-the-type-of-instance-variable-in-class/1181/3 "2019-09-30T13:55:06Z")

</div>

hello @orangeSi

Do you want name to be a `String` or an `Int32`?

---

<div class="post-metadata">

**Author:** ![orangeSi](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/orangesi/32/1569_2.png) [@orangeSi](https://forum.crystal-lang.org/u/orangeSi)\
**Post date:** [September 30, 2019, 1:59pm UTC](https://forum.crystal-lang.org/t/cant-infer-the-type-of-instance-variable-in-class/1181/4 "2019-09-30T13:59:31Z")

</div>

yes, it works! or use `property name : String|Int32 = 3` also works~  
It seems `@` variable in class should be defined in initialize or property, I got it~  
Thanks~

---

<div class="post-metadata">

**Author:** ![orangeSi](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/orangesi/32/1569_2.png) [@orangeSi](https://forum.crystal-lang.org/u/orangeSi)\
**Post date:** [September 30, 2019, 2:02pm UTC](https://forum.crystal-lang.org/t/cant-infer-the-type-of-instance-variable-in-class/1181/5 "2019-09-30T14:02:32Z")

</div>

In fact, I want `name` be `Int32|String` when as argument of go method, but inside of go method I want `name` to be `String`.

---

<div class="post-metadata">

**Author:** ![girng\_github](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/girng_github/32/571_2.png) [@girng\_github](https://forum.crystal-lang.org/u/girng_github)\
**Post date:** [September 30, 2019, 2:26pm UTC](https://forum.crystal-lang.org/t/cant-infer-the-type-of-instance-variable-in-class/1181/6 "2019-09-30T14:26:03Z")

</div>

Interesting!! Thanks I was just curious lol

Glad you got it working!

---

<div class="post-metadata">

**Author:** ![orangeSi](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/orangesi/32/1569_2.png) [@orangeSi](https://forum.crystal-lang.org/u/orangeSi)\
**Post date:** [September 30, 2019, 2:51pm UTC](https://forum.crystal-lang.org/t/cant-infer-the-type-of-instance-variable-in-class/1181/7 "2019-09-30T14:51:55Z")

</div>

I tried this

```auto
class Hello
  alias KK = Int32|Int64|Array(String)
  property name2 : KK = 32
  def initialize()
  end
  def go()
	  @name2 = Array(String).new if @name2.is_a?(Int32)
  end
end

p = Hello.new
p.go()

```

that works, but when I tried this:

```auto
class Hello
  alias KK = Int32|Int64|Array(String)
  property name2 : KK = 32
  def initialize()
  end
  def go()
    puts @name2 > 1 if @name2.is_a?(Int32)
  end
end

p = Hello.new
p.go()

```

got error:

```auto
In tmp6.cr:8:17

 8 | puts @name2 > 1 if @name2.is_a?(Int32)
                 ^
Error: no overload matches 'Array(String)#>' with type Int32

Overloads are:
 - Comparable(T)#>(other : T)

```

---

<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:** [September 30, 2019, 3:02pm UTC](https://forum.crystal-lang.org/t/cant-infer-the-type-of-instance-variable-in-class/1181/8 "2019-09-30T15:02:42Z")

</div>

Since `@name2` can be any type in `Int32|Int64|Array(String)`, this error is catching that there isn’t a method defined that allows you to compare an `Array(String)` with an `Int32`.

You would have to check the type of `@name2` before doing your comparison. For example [https://play.crystal-lang.org/#/r/7o5a](https://play.crystal-lang.org/#/r/7o5a)

```auto
class Hello 
  def initialize(@name2 : Int32 | Int64 | Array(String) = 32); end
  
  def go
    unless (name = @name2).is_a? Array(String)
      puts "Comparison: #{name > 1} - Type: #{typeof(name)}"
    else
      puts "Else: #{typeof(@name2)}"
    end
  end
end

Hello.new.go # => Comparison: true - Type: (Int32 | Int64)
Hello.new(["foo"]).go # => Else: (Array(String) | Int32 | Int64)

```

EDIT: See [https://crystal-lang.org/reference/syntax\_and\_semantics/if\_varis\_a.html](https://crystal-lang.org/reference/syntax_and_semantics/if_varis_a.html)

---

<div class="post-metadata">

**Author:** ![orangeSi](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/orangesi/32/1569_2.png) [@orangeSi](https://forum.crystal-lang.org/u/orangeSi)\
**Post date:** [September 30, 2019, 3:20pm UTC](https://forum.crystal-lang.org/t/cant-infer-the-type-of-instance-variable-in-class/1181/9 "2019-09-30T15:20:56Z")

</div>

> [@orangeSi](#):
>
> name2

got it!  
`is_a` do a little bit different thing with `@variable` and `variable`  
Thanks a lot~
