Ben Hamner
Ben Hamner

Reputation: 4735

Is there a way to check whether a type is iterable within Julia's type system?

Collections in Julia need to support three functions to be iterable: start, next, and done.

Is there currently a way to express that an input x to a function needs to be iterable within the type system? If not, is this on the roadmap?

Upvotes: 14

Views: 1398

Answers (1)

IainDunning
IainDunning

Reputation: 11664

Is it possible? Yes, but not in a very elegant way:

julia> x = 1:5
1:5

julia> applicable(start, x)
true

julia> applicable(next,x,start(x))
true

julia> applicable(done,x,start(x))
true

I'm not sure how to get around the need for the start(x)s - maybe checking for start will be "good enough".

As for a more sensible way, this would need multiple inheritance or interfaces, which is under discussion, e.g. here is one issue, it links to others.

Upvotes: 11

Related Questions