# How can I duplicate class inheriting Array with keeping it's type?

**URL:** <https://forum.crystal-lang.org/t/how-can-i-duplicate-class-inheriting-array-with-keeping-its-type/3628>\
**Category:** Help & Support\
**Created:** [August 22, 2021, 2:59am UTC](https://forum.crystal-lang.org/t/how-can-i-duplicate-class-inheriting-array-with-keeping-its-type/3628 "2021-08-22T02:59:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kanezoh](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kanezoh/32/1213_2.png) [@Kanezoh](https://forum.crystal-lang.org/u/Kanezoh)\
**Post date:** [August 22, 2021, 2:59am UTC](https://forum.crystal-lang.org/t/how-can-i-duplicate-class-inheriting-array-with-keeping-its-type/3628/1 "2021-08-22T02:59:05Z")

</div>

Hello.  
I have trouble in duplicationg class inheriting Array. Here is a sample code.

```auto
class Foo; end

class Bar < Array(Foo)
end

bar = Bar.new
bar2 = bar.dup
puts typeof(bar2) # => Array(Foo)

```

bar2’s type becomes Array(Foo). I know it’s natural because `bar.dup` calls Array#dup.  
But, I want bar2’s type to be Bar.  
Are there any solution to duplicate and keep it’s type `Bar` ?

Thanks.

---

<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, 3:06am UTC](https://forum.crystal-lang.org/t/how-can-i-duplicate-class-inheriting-array-with-keeping-its-type/3628/2 "2021-08-22T03:06:13Z")

</div>

I would advise against inheriting any of the stdlib’s primitive types, such as Array, or Hash, etc.

A better option would be to use a decorator, e.g.

```auto
class Foo; end

class Bar
  forward_missing_to @array

  def initialize(@array : Array(Foo)); end
end

```

Alternatively, depending more on your use case, an alias might make more sense?

```auto
alias Bar = Array(Foo)

```

I do remember seeing an issue about this however. I’ll see if i can find it.

EDIT: I can’t

---

<div class="post-metadata">

**Author:** ![Kanezoh](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kanezoh/32/1213_2.png) [@Kanezoh](https://forum.crystal-lang.org/u/Kanezoh)\
**Post date:** [August 22, 2021, 11:11am UTC](https://forum.crystal-lang.org/t/how-can-i-duplicate-class-inheriting-array-with-keeping-its-type/3628/3 "2021-08-22T11:11:57Z")

</div>

> I would advise against inheriting any of the stdlib’s primitive types

I see. When I ran into this problem, I felt the way of inheriting Array may be not good.  
Now, I’m going to take first approach, letting Bar have Array instance and delegate certain method to it.

Thank you!
