# Store class(T) in an array

**URL:** <https://forum.crystal-lang.org/t/store-class-t-in-an-array/6372>\
**Category:** Help & Support\
**Created:** [January 26, 2024, 8:34pm UTC](https://forum.crystal-lang.org/t/store-class-t-in-an-array/6372 "2024-01-26T20:34:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hutou](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hutou/32/1337_2.png) [@hutou](https://forum.crystal-lang.org/u/hutou)\
**Post date:** [January 26, 2024, 8:34pm UTC](https://forum.crystal-lang.org/t/store-class-t-in-an-array/6372/1 "2024-01-26T20:34:28Z")

</div>

I’d like to store the successive instances of a generic class in an array.

The following code doesn’t compile, which is expected.

```crystal
class Test(T)
  class_property instances = [] of Test(T) # => Error ! 
  def initialize(@var : T)
  end
end

x = Test.new(33)
y = Test.new("abc")

```

> Error: can’t infer the type parameter T for the generic class Test(T). Please provide it explicitly

Is there any way of doing this, given that type T is not known in advance?

---

<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:** [January 26, 2024, 8:38pm UTC](https://forum.crystal-lang.org/t/store-class-t-in-an-array/6372/2 "2024-01-26T20:38:24Z")

</div>

I think the problem is `T` isn’t known in the class scope, only in the instance scope when instantiated. Workaround would be like add some abstract non-generic class and inherit from that. Then type the array using that abstract parent type.

---

<div class="post-metadata">

**Author:** ![hutou](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hutou/32/1337_2.png) [@hutou](https://forum.crystal-lang.org/u/hutou)\
**Post date:** [January 26, 2024, 8:56pm UTC](https://forum.crystal-lang.org/t/store-class-t-in-an-array/6372/3 "2024-01-26T20:56:59Z")

</div>

Yes, it works! Thanks a lot @Blacksmoke16 !  
Here is the modified code:

```crystal
abstract class ATest
  class_property instances = [] of ATest
end

class Test(T) < ATest
  def initialize(@var : T)
    ATest.instances << self
  end
end

x = Test.new(33)
y = Test.new("abc")

p! ATest.instances

```

which outputs:

> ATest.instances # =\> [#\<Test(Int32):0x7b9eff8bff30 @var=33\>, #\<Test(String):0x7b9eff8be580 @var=“abc”\>]

```auto

```

---

<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:** [January 26, 2024, 8:58pm UTC](https://forum.crystal-lang.org/t/store-class-t-in-an-array/6372/4 "2024-01-26T20:58:02Z")

</div>

You can keep the class property in `Test(T)`, you just need `ATest` for typing it.

---

<div class="post-metadata">

**Author:** ![hutou](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hutou/32/1337_2.png) [@hutou](https://forum.crystal-lang.org/u/hutou)\
**Post date:** [January 26, 2024, 9:08pm UTC](https://forum.crystal-lang.org/t/store-class-t-in-an-array/6372/5 "2024-01-26T21:08:07Z")

</div>

Hmm, moving `class_property instances` from `Atest` to `Test(T)` gives an error message:

> Error: undefined method ‘instances’ for ATest.class

---

<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:** [January 26, 2024, 9:09pm UTC](https://forum.crystal-lang.org/t/store-class-t-in-an-array/6372/6 "2024-01-26T21:09:44Z")

</div>

Class methods aren’t visible by default in the instance scope. Could do like `self.class.instances`, or just access it directly `@@instances << self`.

---

<div class="post-metadata">

**Author:** ![hutou](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hutou/32/1337_2.png) [@hutou](https://forum.crystal-lang.org/u/hutou)\
**Post date:** [January 26, 2024, 9:20pm UTC](https://forum.crystal-lang.org/t/store-class-t-in-an-array/6372/7 "2024-01-26T21:20:56Z")

</div>

Yes, it works with both syntaxes.  
Thank again.
