Stream: helpdesk (published)

Topic: Iterable argument to function


view this post on Zulip Alec (Jun 01 2022 at 03:19):

Is this answer on Stack overflow up to date? Is there a way to dispatch on whether an argument is iteratble? If not dispatch, is the given answer still the best practice? I would think not since there's mutable(?)/stateful(?) iterators where you can destroy elements by iterating, right?

view this post on Zulip Jakob Nybo Nissen (Jun 01 2022 at 07:57):

There is, unfortunately, no good way. Julia does not have a real trait system for this.
You can use hasmethod(iterate, (T,)), but that is dynamic. The package Traits.jl has static_hasmethod, but I wouldn't rely on it to be stable.

view this post on Zulip Sukera (Jun 01 2022 at 12:31):

There is a Base.isiterable, but that also just calls either hasmethod or applicable

view this post on Zulip Sukera (Jun 01 2022 at 12:31):

there's no really good static way to do this today

view this post on Zulip Jakob Nybo Nissen (Jun 01 2022 at 12:53):

It would be really cool to have a way to dispatch on this, i.e. a way to automatically get traits from the presence/absence of methods.
Of course, if function a dispatches on whether a method of b exists, that requires a backedge from b to a.


Last updated: Oct 02 2023 at 04:34 UTC