# How do I define a function whose argument is iterable?

**URL:** <https://discourse.julialang.org/t/how-do-i-define-a-function-whose-argument-is-iterable/42484>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [July 3, 2020, 4:33pm UTC](https://discourse.julialang.org/t/how-do-i-define-a-function-whose-argument-is-iterable/42484 "2020-07-03T16:33:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SatyaKomatineni](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@SatyaKomatineni](https://discourse.julialang.org/u/SatyaKomatineni)\
**Post date:** [July 3, 2020, 4:33pm UTC](https://discourse.julialang.org/t/how-do-i-define-a-function-whose-argument-is-iterable/42484/1 "2020-07-03T16:33:05Z")

</div>

function printItems(iter)  
#print or do something with each item  
end

How do I say “iter” is of type that can be iterated on? I don’t want to say for example AbstractArray because anything iteratable will work.

Note: I do understand one can use other ways like foreach() etc.

But I am question is about the “type” of specifying something as “iterable” or “iteratable”

Or is that implied with “any”

Appreciate your time

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [July 3, 2020, 4:36pm UTC](https://discourse.julialang.org/t/how-do-i-define-a-function-whose-argument-is-iterable/42484/2 "2020-07-03T16:36:23Z")

</div>

I think a fruitful transform of your query would be: design a function that does its thing on a single item, and then use broadcast.

```julia
pr(x) = println("$x")
a = (1, 2, 3.0)
pr.(a)

```

---

<div class="post-metadata">

**Author:** ![SatyaKomatineni](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@SatyaKomatineni](https://discourse.julialang.org/u/SatyaKomatineni)\
**Post date:** [July 3, 2020, 4:41pm UTC](https://discourse.julialang.org/t/how-do-i-define-a-function-whose-argument-is-iterable/42484/3 "2020-07-03T16:41:36Z")

</div>

Thank you.

Ignore my function for a minute.

May be my direct question should be

“is there a base type that allows iteration”

Any collection can be iterated. Even non collections can implement iteration. Can a function specify such a constraint.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [July 3, 2020, 4:58pm UTC](https://discourse.julialang.org/t/how-do-i-define-a-function-whose-argument-is-iterable/42484/4 "2020-07-03T16:58:23Z")

</div>

No, it should just be `printitems(iter)` accepting type `::Any`.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 3, 2020, 6:05pm UTC](https://discourse.julialang.org/t/how-do-i-define-a-function-whose-argument-is-iterable/42484/5 "2020-07-03T18:05:40Z")

</div>

I understand that you can want to verify this so you can fail as soon as possible, what is often a good practice. The majority of the Julia code, however, will probably just receive `Any` and let the exception be raised if the object cannot be iterated.

Note, however, that, while @mcabbott is correct, i.e., there is no base type that allows iteration, you can write a conditional checking if the object allows iteration, you just check with [`applicable`](https://docs.julialang.org/en/v1/base/base/#Core.applicable)`(`[`iterate`](https://docs.julialang.org/en/v1/base/collections/#Base.iterate)`, object)`.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [July 3, 2020, 6:19pm UTC](https://discourse.julialang.org/t/how-do-i-define-a-function-whose-argument-is-iterable/42484/6 "2020-07-03T18:19:33Z")

</div>

There was some discussion of this recently here: [What's the best way to dispatch on an \_iterable\_ trait?](https://discourse.julialang.org/t/whats-the-best-way-to-dispatch-on-an-iterable-trait/42084).

The TLDR is: yes, you can do this (and you can do it efficiently), but there’s probably a better way to get what you want.
