January 2020
Intermediate to advanced
532 pages
13h 31m
English
The Julia Base library uses traits quite extensively. An example of such a trait is Base.IteratorSize. Its definition can be found using generator.jl:
abstract type IteratorSize endstruct SizeUnknown <: IteratorSize endstruct HasLength <: IteratorSize endstruct HasShape{N} <: IteratorSize endstruct IsInfinite <: IteratorSize end
This trait is slightly different from what we have learned about so far because it is not binary. The IteratorSize trait can be SizeUnknown, HasLength, HasShape{N}, or IsInfinite. The IteratorSize function is defined as follows:
""" IteratorSize(itertype::Type) -> IteratorSize"""IteratorSize(x) = IteratorSize(typeof(x))IteratorSize(::Type) = HasLength() # HasLength is the defaultIteratorSize(::Type{<:AbstractArray{<:Any,N}}) ...Read now
Unlock full access