# Abstract each method error

**URL:** <https://forum.crystal-lang.org/t/abstract-each-method-error/5373>\
**Category:** Help & Support\
**Created:** [February 11, 2023, 6:12pm UTC](https://forum.crystal-lang.org/t/abstract-each-method-error/5373 "2023-02-11T18:12:19Z")\
**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:** [February 11, 2023, 6:12pm UTC](https://forum.crystal-lang.org/t/abstract-each-method-error/5373/1 "2023-02-11T18:12:19Z")

</div>

The following code

```crystal
abstract class Test
  abstract def each
end

class MyTest < Test
  def initialize(@ary : Array(Int32))
  end
  def each
    @ary.each do |e|
      yield e
    end
  end
end

```

displays the error:

> 5 | class MyTest \< Test  
> ^  
> Error: abstract `def Test#each()` must be implemented by MyTest

Hmm, 🤔 what is the problem with this code ?

---

<div class="post-metadata">

**Author:** ![nogginly](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/nogginly/32/1795_2.png) [@nogginly](https://forum.crystal-lang.org/u/nogginly)\
**Post date:** [February 11, 2023, 6:22pm UTC](https://forum.crystal-lang.org/t/abstract-each-method-error/5373/2 "2023-02-11T18:22:41Z")

</div>

Try the following in `Test` abstract class:

```plaintext
  abstract def each(& : T ->)

```

Since the implementation `MyTest#each` has a `yield`, it changes the inferred “type signature” of the method, which then no longer matches the vanilla `abstract def each` which does not declare that the implementation will `yield` (and thus require a block when called.)

(Btw, I was surprised that I could use `T` in that abstract class that is not explicitly “generic”; so there is some nuance to the type inference that I don’t yet understand. 🤩)

---

<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:** [February 11, 2023, 7:18pm UTC](https://forum.crystal-lang.org/t/abstract-each-method-error/5373/3 "2023-02-11T19:18:00Z")

</div>

The important part is designating the abstract method to be yielding. The given signature corresponds to a non-yielding method, but you only implementation does yield.

The correct signature for that would be `abstract def each(& : Int32 ->)`.  
The version with `T` works, but I think that’s more of an accident. The abstract method is never instantiated, so the compiler doesn’t need to resolve the type. If it would, it would discover that it doesn’t exist. But it’s enough to inform the abstract method def checker 🤷

---

<div class="post-metadata">

**Author:** ![nogginly](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/nogginly/32/1795_2.png) [@nogginly](https://forum.crystal-lang.org/u/nogginly)\
**Post date:** [February 11, 2023, 7:27pm UTC](https://forum.crystal-lang.org/t/abstract-each-method-error/5373/4 "2023-02-11T19:27:25Z")

</div>

Is there a way, besides making `Test` explicitly generic, to declare the abstract method’s yield/block without a concrete type (i.e. `Int32`) so that a future class can yield anything?

```plaintext
  ## this does not compile
  abstract def each(& ->)

```

I’m more curious than anything else as I’ve been able to declare a method arg without a type and have it aggregate all future calls’ arg types (which is pretty nifty).

---

<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:** [February 11, 2023, 7:54pm UTC](https://forum.crystal-lang.org/t/abstract-each-method-error/5373/5 "2023-02-11T19:54:17Z")

</div>

I think it would just be `abstract def each(&)`.

---

<div class="post-metadata">

**Author:** ![nogginly](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/nogginly/32/1795_2.png) [@nogginly](https://forum.crystal-lang.org/u/nogginly)\
**Post date:** [February 11, 2023, 8:54pm UTC](https://forum.crystal-lang.org/t/abstract-each-method-error/5373/6 "2023-02-11T20:54:36Z")

</div>

That was the first thing I tried and it did not work. I’ve simplified the example below and identified what I tried.

```plaintext
abstract class Test
  abstract def each(& : Int32 ->) # this works
  #abstract def each(&) # this does not
  #abstract def each(& ->) # this does not
  #abstract def each # this does not
end

class MyTest < Test
  def each
    yield 10
  end
end

```

Interestingly, as my first attempt using `T` showed, that type could be anything, as the specific type isn’t as important as the structure of the declaration which sets up the abstract method definition for later type checking whe implementing the subclass.

_(One of the fascinating things about Crystal is how the compiler defers some syntax/semantic checking until when a method needs to have its code generated. This is tree-shaking at its best.)_

---

<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:** [February 11, 2023, 10:13pm UTC](https://forum.crystal-lang.org/t/abstract-each-method-error/5373/7 "2023-02-11T22:13:31Z")

</div>

Okay, I think i see what’s going on. `each(&)` is technically not matched with your implementation because it is yielding a value where the abstract def is just looking for a method that yields, but with no args. In the case of wanting to restrict the method to yield _something_, you could do `abstract def each(& : _ ->)` which basically says it yields one argument of any type, and returns `nil`. E.g. `yield 10` or `yield "foo"` would satisfy it, but not `yield 10, "foo"` (two arguments).
