# Passing an array of Procs to a function

**URL:** <https://forum.crystal-lang.org/t/passing-an-array-of-procs-to-a-function/6305>\
**Category:** Help & Support\
**Created:** [January 11, 2024, 5:45pm UTC](https://forum.crystal-lang.org/t/passing-an-array-of-procs-to-a-function/6305 "2024-01-11T17:45:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aboulafia](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@aboulafia](https://forum.crystal-lang.org/u/aboulafia)\
**Post date:** [January 11, 2024, 5:45pm UTC](https://forum.crystal-lang.org/t/passing-an-array-of-procs-to-a-function/6305/1 "2024-01-11T17:45:20Z")

</div>

Ok, last question for today (I don’t want to flood ;-) )

I’ve this

```auto
walk(@program, ->transform_literal(AST::Node))
walk(@program, ->check_ident_type(AST::Node))

def walk(node : AST::Node, fn)
    fn.call(node)
    node.children.each do |child|
      walk(child, fn)
    end
end

```

Not very smart because I walk the AST tree twice

So I tried

```auto
operations = Array(-> (AST::Node)).new
operations << ->transform_literal(AST::Node)
operations << ->check_ident_type(AST::Node)
walk(@program, operations)

def walk(node : AST::Node, fns : Array(-> (AST::Node)))
end

```

But I get a compilation error on `operations << ->transform_literal(AST::Node)`

`Error: expected argument #1 to 'Array(Proc(AST::Node))#<<' to be Proc(AST::Node), not Proc(AST::Node, (TypeBase | Nil))`

And I don’t understand it

---

<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:** [January 11, 2024, 6:13pm UTC](https://forum.crystal-lang.org/t/passing-an-array-of-procs-to-a-function/6305/2 "2024-01-11T18:13:33Z")

</div>

The proc that you’re trying to add has the input type `AST::Node` and output type `TypeBase | Nil`.  
That doesn’t fit into an array which expects a proc with no input and output type `AST::Node`. Just adjust the array type accordingly and you should be good.

---

<div class="post-metadata">

**Author:** ![aboulafia](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@aboulafia](https://forum.crystal-lang.org/u/aboulafia)\
**Post date:** [January 11, 2024, 6:21pm UTC](https://forum.crystal-lang.org/t/passing-an-array-of-procs-to-a-function/6305/3 "2024-01-11T18:21:03Z")

</div>

I don’t get it, the `transform_literal` proc I’m trying to add is defined as

```auto
def transform_literal(node : AST::Node)
end

```

There’s no `TypeBase | Nil` as output type

---

<div class="post-metadata">

**Author:** ![yxhuvud](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/yxhuvud/32/27_2.png) [@yxhuvud](https://forum.crystal-lang.org/u/yxhuvud)\
**Post date:** [January 11, 2024, 6:27pm UTC](https://forum.crystal-lang.org/t/passing-an-array-of-procs-to-a-function/6305/4 "2024-01-11T18:27:24Z")

</div>

If that is the literal definition, it would be typed as AST::Node → Nil

---

<div class="post-metadata">

**Author:** ![aboulafia](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@aboulafia](https://forum.crystal-lang.org/u/aboulafia)\
**Post date:** [January 11, 2024, 7:05pm UTC](https://forum.crystal-lang.org/t/passing-an-array-of-procs-to-a-function/6305/5 "2024-01-11T19:05:25Z")

</div>

But my `transform_literal` function is not intended to return Nils, it’s a return Void function…

Sorry, that’s me but I don’t understand

Does `Array(-> (AST::Node))` mean : an Array of Proc expecting an `AST:Node` parameter type on input and `Void` type on output ?

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [January 11, 2024, 7:29pm UTC](https://forum.crystal-lang.org/t/passing-an-array-of-procs-to-a-function/6305/6 "2024-01-11T19:29:59Z")

</div>

Try `Array(AST::Node ->)`.

> [@aboulafia](#):
>
> But my `transform_literal` function is not intended to return Nils, it’s a return Void function…

There’s no useful distinction in Crystal between `Void` and `Nil` types. If your proc doesn’t return a value, it’s `nil`.

```crystal
def call
end

value = call
pp typeof(value) # Nil

```

---

<div class="post-metadata">

**Author:** ![aboulafia](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@aboulafia](https://forum.crystal-lang.org/u/aboulafia)\
**Post date:** [January 11, 2024, 8:00pm UTC](https://forum.crystal-lang.org/t/passing-an-array-of-procs-to-a-function/6305/7 "2024-01-11T20:00:38Z")

</div>

Thanks ! That works

I’ve been fooled by the fact that a Proc is defined as

```auto
proc = ->(x : Int32, y : Int32) { x + y }
proc.call(1, 2) # => 3

```

In which → is followed by the input parameter types  
whilst in my case → is the return type

A lot still to learn, obviously ;-)

---

<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:** [January 11, 2024, 9:39pm UTC](https://forum.crystal-lang.org/t/passing-an-array-of-procs-to-a-function/6305/8 "2024-01-11T21:39:09Z")

</div>

> [@aboulafia](#):
>
> In which → is followed by the input parameter types  
> whilst in my case → is the return type
> 
> A lot still to learn, obviously ;-)

Good point. That can be a bit confusing.
