# Length from iterator?

**URL:** <https://discourse.julialang.org/t/length-from-iterator/27821>\
**Category:** New to Julia\
**Tags:** iterators\
**Created:** [August 21, 2019, 10:20pm UTC](https://discourse.julialang.org/t/length-from-iterator/27821 "2019-08-21T22:20:07Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [August 22, 2019, 11:17am UTC](https://discourse.julialang.org/t/length-from-iterator/27821/22 "2019-08-22T11:17:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![traktofon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/traktofon/32/591_2.png) [@traktofon](https://discourse.julialang.org/u/traktofon)\
**Post date:** [August 22, 2019, 11:22am UTC](https://discourse.julialang.org/t/length-from-iterator/27821/23 "2019-08-22T11:22:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rapus95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapus95/32/3773_2.png) [@rapus95](https://discourse.julialang.org/u/rapus95)\
**Post date:** [August 27, 2019, 7:40pm UTC](https://discourse.julialang.org/t/length-from-iterator/27821/24 "2019-08-27T19:40:42Z")

</div>

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](https://github.com/JuliaLang/julia/issues/22279) for that which is open since 2017.

[Previous page](https://discourse.julialang.org/t/length-from-iterator/27821.md?page=1)
