# Why is this failing?

**URL:** <https://forum.crystal-lang.org/t/why-is-this-failing/7161>\
**Category:** Help & Support\
**Created:** [September 1, 2024, 11:42pm UTC](https://forum.crystal-lang.org/t/why-is-this-failing/7161 "2024-09-01T23:42:12Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralsina](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ralsina/32/2139_2.png) [@ralsina](https://forum.crystal-lang.org/u/ralsina)\
**Post date:** [September 1, 2024, 11:42pm UTC](https://forum.crystal-lang.org/t/why-is-this-failing/7161/1 "2024-09-01T23:42:12Z")

</div>

I am having problems using these two shards at once: docopt and reply

If this is shard.yml

```yaml
name: cascara
version: 0.1.0

authors:
  - Roberto Alsina <roberto.alsina@gmail.com>

targets:
  cascara:
    main: src/cascara.cr

dependencies:
  docopt:
    github: chenkovsky/docopt.cr
  reply:
    github: I3oris/reply
    branch: main

crystal: '>= 1.13.1'

license: MIT

```

This code fails to build as soon as I uncomment the `require "docopt"`:

```crystal
require "reply"
require "docopt"

class Shell < Reply::Reader
end

reader = Shell.new

reader.read_loop do |expression|
  puts " => #{expression}"
end

```

With this error:

```auto
In lib/reply/src/auto_completion.cr:33:21

 33 | def initialize(&@auto_complete : String, String -> {String, Array(String)})
                      ^-------------
Error: instance variable '@auto_complete' of Reply::AutoCompletion must be Proc(String, String, Tuple(String, Array(String))), not Proc(String, String, Tuple(String, Array(String)))

```

I am totally at a loss on how this can fail like this (notice both types are the same?)

---

<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 2, 2024, 12:24am UTC](https://forum.crystal-lang.org/t/why-is-this-failing/7161/2 "2024-09-02T00:24:30Z")

</div>

Reduced, probably could go further, but wanted to keep things somewhat close to how they are in the shards:

```crystal
module Reply
  private class AutoCompletion
    def initialize(&@auto_complete : String, String -> {String, Array(String)})
    end
  end

  class Reader
    @auto_completion : AutoCompletion

    def initialize
      @auto_completion = AutoCompletion.new(&->auto_complete(String, String))
    end

    def auto_complete(current_word : String, expression_before : String)
      return "", [] of String
    end
  end
end

module Docopt
  class Tokens < Array(String)
  end
end

Reply::Reader.new

```

The gist of it is `docopt` is doing a bad thing and inheriting from `Array`, which is breaking the compiler. I’m 90% sure there is an issue about this somewhere, but I’d have to try and dig it up.

The solution here is probably for `docopt` to not to do that anymore.

EDIT: [When defining subclass of Array, unrelated code stops compiling · Issue #7447 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/7447) & [Error when nested array/hash is used on inheritance · Issue #10133 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/10133)

---

<div class="post-metadata">

**Author:** ![ralsina](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ralsina/32/2139_2.png) [@ralsina](https://forum.crystal-lang.org/u/ralsina)\
**Post date:** [September 2, 2024, 1:12am UTC](https://forum.crystal-lang.org/t/why-is-this-failing/7161/3 "2024-09-02T01:12:46Z")

</div>

Nice!

Could the compiler prevent that like it prevents inheriting String?

---

<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 2, 2024, 1:20am UTC](https://forum.crystal-lang.org/t/why-is-this-failing/7161/4 "2024-09-02T01:20:27Z")

</div>

Possibly, but idk if it really makes sense to do that to _every_ stdlib type where it’s not expected you do that. Is worth discussing at least. Maybe the real solution is fix the bug, but don’t explicitly prevent this like String. IIRC with String it was a more special type compared to Array and some of the other stdlib types.

---

<div class="post-metadata">

**Author:** ![HertzDevil](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hertzdevil/32/1023_2.png) [@HertzDevil](https://forum.crystal-lang.org/u/HertzDevil)\
**Post date:** [September 2, 2024, 1:30am UTC](https://forum.crystal-lang.org/t/why-is-this-failing/7161/5 "2024-09-02T01:30:08Z")

</div>

This is [No overload error with proc using alias of class thats extended after creation · Issue #11734 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/11734), and it happens with any non-abstract base class, not just `Array`.

---

<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 2, 2024, 1:30am UTC](https://forum.crystal-lang.org/t/why-is-this-failing/7161/6 "2024-09-02T01:30:53Z")

</div>

Oh nice, thanks. I don’t even remember making that one 😅.

---

<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:** [September 2, 2024, 8:00am UTC](https://forum.crystal-lang.org/t/why-is-this-failing/7161/7 "2024-09-02T08:00:01Z")

</div>

> [@Blacksmoke16](#):
>
> The gist of it is `docopt` is doing a bad thing and inheriting from `Array`, which is breaking the compiler.

Wired, does our official doc say we’re not supposed to do this? inheriting from an Array is seem a very common operation? how to achieve the same goal without using inheritance?

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [September 2, 2024, 12:52pm UTC](https://forum.crystal-lang.org/t/why-is-this-failing/7161/8 "2024-09-02T12:52:17Z")

</div>

> [@zw963](#):
>
> inheriting from an Array is seem a very common operation? how to achieve the same goal without using inheritance?

I don’t think that’s a common thing. Why would you want to inherit from `Array`? It fully encases a concept and is basically feature-complete. So I don’t see any reason for inheritance.  
If you want to build some custom behaviour on top of `Array`, you can use composition, i.e. a type with an instance variable of type `Array`. If you want it to behave roughly similar to `Array`, you may include `Indexable` and `Indexable::Mutable`.

---

<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 2, 2024, 1:03pm UTC](https://forum.crystal-lang.org/t/why-is-this-failing/7161/9 "2024-09-02T13:03:04Z")

</div>

> [@zw963](#):
>
> Wired, does our official doc say we’re not supposed to do this?

It’s not really a Crystal specific thing that says why you shouldn’t, but more of a general design reason. It’s usually not a good practice to inherit types you do not own, which accounts for more than just `Array`, and beyond the stdlib itself.

---

<div class="post-metadata">

**Author:** ![ralsina](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ralsina/32/2139_2.png) [@ralsina](https://forum.crystal-lang.org/u/ralsina)\
**Post date:** [September 2, 2024, 2:35pm UTC](https://forum.crystal-lang.org/t/why-is-this-failing/7161/10 "2024-09-02T14:35:27Z")

</div>

TL;DR: I have made a PR to fix this in docopt.cr

> <https://github.com/chenkovsky/docopt.cr/pull/11>
>
> Having Tokens inherit Array(String) was breaking unrelated code because of https…://github.com/crystal-lang/crystal/issues/11734
> 
> This PR removes the inheritance and replaces it with composition and usage of the delegate macro

However, since that shard seems to be abandoned, if someone wants to use docopt I promise to keep my fork `ralsina/docopt.cr` with at least bugfixes.

I may even fix the only open issue :-D

---

<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:** [September 7, 2024, 4:37am UTC](https://forum.crystal-lang.org/t/why-is-this-failing/7161/11 "2024-09-07T04:37:54Z")

</div>

I am not saying that in a bad way, but that why I try to avoid to use any shards, I don’t want to base my work on a code will be not maintain anymore

---

<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 7, 2024, 1:40pm UTC](https://forum.crystal-lang.org/t/why-is-this-failing/7161/12 "2024-09-07T13:40:09Z")

</div>

> [@Fulgurance](#):
>
> but that why I try to avoid to use any shards

My 2 cents on this it’s a balancing act. I’m also in the camp of trying to avoid third-party dependencies if possible, but also do not think it’s a great idea to be absolute in _never_ using a third-party shard. Writing something yourself instead of using a preexisting shard introduces its own problems. I.e. you’re now responsible for maintaining this extra code, making sure it’s secure, correct, etc. versus deferring that to the shard owner.

On the flip side, I think it’s important to be cognizant of what shards you depend on. This includes knowing who is the owner/maintainer, the status of the shard, following the changelog to be aware of upcoming changes, having a plan for what happens if this shard disappears or goes EoL, etc.

This is getting a bit off-topic tho, so might be best to create a new thread if you want to discuss this further.

---

<div class="post-metadata">

**Author:** ![ralsina](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ralsina/32/2139_2.png) [@ralsina](https://forum.crystal-lang.org/u/ralsina)\
**Post date:** [September 8, 2024, 12:02pm UTC](https://forum.crystal-lang.org/t/why-is-this-failing/7161/13 "2024-09-08T12:02:24Z")

</div>

I use shards if I am willing to fix bugs in them and/or adopt them. It’s part of using a “small” platform. If nobody uses shards there are no shards, and the ocean’s too big to boil by myself.

For example, in a project I am using a VERY nice docker API shard called docr.

I found a couple minor bugs and a larger one. I just fixed them, they got merged a couple months later.

If I had not used it everyone would either not be able to use the docker API or have a buggier shard.

If the author had not written it, that project (which I use and wrote for myself) would not exist.

So sure, a balancing act, but I _try_ to err on the generous side.

---

<div class="post-metadata">

**Author:** ![SlayerShadow](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/slayershadow/32/2770_2.png) [@SlayerShadow](https://forum.crystal-lang.org/u/SlayerShadow)\
**Post date:** [January 14, 2025, 8:08pm UTC](https://forum.crystal-lang.org/t/why-is-this-failing/7161/14 "2025-01-14T20:08:48Z")

</div>

> [@straight-shoota](#):
>
> Why would you want to inherit from `Array`?

I used this to create a simple Matrix class for linear algebra which is 2D array in my case. Due to the issue instead of array inheritance i used a raw class `Matrix(T)` with internal `@buffer` object which is exactly `Array( Array ( T ) )`.
