Length from iterator?

An iterator which returns Base.HasLength() even though there is not be a length method will fail during collect, so this query is reliable.

Agreed, the behaviour is “reliable” in the sense that it will reliably error if length is not defined. Perhaps I should have said that the interface is not quite intuitive (at least for me). I understand this can’t be changed within Julia 1.x.

Why do we not have SizeUnknown() as default case? Because if you intend to use the length function you would have to define it yourself and in that case you would define IteratorSize properly as some sort of general workflow (that’s the way if you want to use size() because you need to then set IteratorSize to HasShape()). But if you do not provide a length function one usually forgets to change IteratorSize to SizeUnknown().
Aka, why the workflow is not like the following:
You want any kind of sizehint? → Define size/length and set IteratorSize properly.
Default: No sizehint.

As far as I understand the base language, Julian style is to use defaults where they can be inferred from other places (like some mathematical relations). And in this case we can’t infer the length. Default cases should not contradict each other. But here we have IteratorSize() default to HasLength() while length() (which is assumed to exist by the default for IteratorSize()) ‘defaults to’ some ‘method not found’ error. This in my opinion contradicts the HasLength().

EDIT: I just found an issue for that which is open since 2017.

2 Likes