# Array allow child class types

**URL:** <https://forum.crystal-lang.org/t/array-allow-child-class-types/3631>\
**Category:** Offtopic\
**Created:** [August 22, 2021, 8:48pm UTC](https://forum.crystal-lang.org/t/array-allow-child-class-types/3631 "2021-08-22T20:48:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [August 22, 2021, 8:48pm UTC](https://forum.crystal-lang.org/t/array-allow-child-class-types/3631/1 "2021-08-22T20:48:31Z")

</div>

Hello, I’m with a problem actually, I have a class CommandLineOption. And I made some others class inherit from CommandLineOption.

CommandLineOption have “option” as parameter, and this parameter is an array with others CommandLineOption items. But how can I allow this array to accept CommandLineOption class and their class are inherit them?

Normally, it’s this:  
`Array(ISM::CommandLineOption).new`

But this Array allow only CommandLineOption, not classes inherit of it

---

<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:** [August 22, 2021, 8:53pm UTC](https://forum.crystal-lang.org/t/array-allow-child-class-types/3631/2 "2021-08-22T20:53:19Z")

</div>

`Array(ISM::CommandLineOption)` allows instances of that type and its children. I think you’re running into [Inheritance - Crystal](https://crystal-lang.org/reference/syntax_and_semantics/inheritance.html#covariance-and-contravariance). But it’s hard to say without some examples/errors.

---

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [August 22, 2021, 9:43pm UTC](https://forum.crystal-lang.org/t/array-allow-child-class-types/3631/3 "2021-08-22T21:43:16Z")

</div>

Hmmmm… yes normally, but it’s strange because I have this bug. It’s my problem actually:

```auto

~/Documents/Programmation/ISM ❯❯❯ crystal Main.cr software install ✘ 1 master ✖ ✱ ◼
Showing last frame. Use --error-trace for full trace.

In ISM/CommandLineOption.cr:17:13

 17 | @options = options
      ^-------
Error: instance variable '@options' of ISM::CommandLineOption must be Array(ISM::CommandLineOption), not Array(ISM::Option::SoftwareInstall)

```

But I’m sure what you said is true, because I have one other class do something similar, and I don’t have this problem.

---

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [August 22, 2021, 9:51pm UTC](https://forum.crystal-lang.org/t/array-allow-child-class-types/3631/4 "2021-08-22T21:51:38Z")

</div>

Normally, I initialize the value of my child class with this:

`[ISM::Option::SoftwareInstall.new]`

But if I don’t do that:  
`[ISM::Option::SoftwareInstall.new] of ISM::CommandLineOption`  
It don’t work. But it’s not very clean (I think) like that.

---

<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:** [August 23, 2021, 12:54am UTC](https://forum.crystal-lang.org/t/array-allow-child-class-types/3631/5 "2021-08-23T00:54:50Z")

</div>

This confirms my suspicion. Unfortunately this is best you can do at the moment. I’d checkout that link I posted for more details.

---

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [August 23, 2021, 11:14pm UTC](https://forum.crystal-lang.org/t/array-allow-child-class-types/3631/6 "2021-08-23T23:14:07Z")

</div>

I think it’s not really proper, no ?

---

<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:** [August 23, 2021, 11:33pm UTC](https://forum.crystal-lang.org/t/array-allow-child-class-types/3631/7 "2021-08-23T23:33:10Z")

</div>

What isn’t proper? As stated in the link, `[ISM::Option::SoftwareInstall.new]` is a `Array(ISM::Option::SoftwareInstall)` even if that type inherits from `ISM::CommandLineOption`. If you want an array of child types to be typed as the parent class you need to use `of ISM::CommandLineOption`. That’s just how things work at the moment.

---

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [August 24, 2021, 4:47am UTC](https://forum.crystal-lang.org/t/array-allow-child-class-types/3631/8 "2021-08-24T04:47:29Z")

</div>

Ah okay. I wasn’t sure. Thanks you very much
