Meaning of Base function source code in julia documentation

Hello community!

I have been looking at source code for some functions and when looking at certain base functions (like shown in the picture) the function is simply defined as itself. How does this make sense? Where is the code that actually perfroms the function?

Best regards


The implementation of iterate depends on the type you provide when calling, e.g.:

julia> s="12345"

julia> @less iterate(s)

@inline function iterate(s::String, i::Int=firstindex(s))
    (i % UInt) - 1 < ncodeunits(s) || return nothing
    b = @inbounds codeunit(s, i)
    u = UInt32(b) << 24
    between(b, 0x80, 0xf7) || return reinterpret(Char, u), i+1
    return iterate_continued(s, i, u)

What you have screenshoted is a function definition with 0 method. It’s like a stub, to provide the anchor for the help text shown when doing ?iterate.

1 Like


That’s a function declaration without an associated method definition i.e. it introduces the name of the function to Julia without defining specific behaviour for a specific set of arguments. It’s useful to

  1. organize documentation for the function separately where it doesn’t get in the way of the code, since any docstring placed above the declaration also becomes part of its documentation. This is a common practice especially in packages, where long explanatory docstrings can easily get in the way of navigating the code without this.
  2. bring the name into scope so that other code in that scope can refer to iterate without needing to define a specific method for it.

The code that performs iteration for Arrays is in array.jl. You can access it with @edit iterate([1], 2).
There are in total 200+ methods for iterate define in base Julia and standard libraries, you can access the others in a similar way too, eg. @edit iterate(1:10, 2), @edit iterate("Hello Julia", 2), etc.