IteratorSize is a Julia collection trait that tells iteration machinery whether the number of elements is known. A collection can report HasLength when length is available, or SizeUnknown when calculating its size is ineffective or slow. section 12.5.1
The trait changes how collect builds its result. With HasLength, Julia calls length and can prepare an appropriately sized array; with SizeUnknown, it starts with an empty array and grows it as elements arrive. The default trait is HasLength, so custom collections need an explicit override when that assumption is unsuitable. section 12.5.1
A staged rocket with unknown size
For a Rocket hierarchy whose number of stages is not efficiently determined, define the trait for every Rocket subtype: import Base: iterate, IteratorSize IteratorSize(::Type{<:Rocket}) = Base.SizeUnknown() The same collection can then support operations such as map(mass, booster) and sum(thrust, booster), while collection-building code grows its output as needed. section 12.5.1 section 12.8
Listing 12.19 Add the SizeUnknown trait to the Rocket subtypes
import Base: iterate, IteratorSize
IteratorSize(::Type{<:Rocket}) = Base.SizeUnknown()
Shows the compact Julia method that assigns SizeUnknown to Rocket subtypes. section 12.5.1
