Problem with @which source code number of lines?

This topic is too deep in language for my, to understand what is really going on, so maybe I wrongly think that is a problem.

When you using

@which sum([1 2 3])

you get line sum(a::AbstractArray) in Base at reducedim.jl:645. This is great feature, especially when you use Jupyter, but this line is (probably) wrong. reducedim.jl:645 is inside for loop below any!(r, A) function and function sum(a::AbstractArray) is absent from this file. Function sum(A::AbstractArray; dims) is at the line 370 of this file.

No, this position is actually correct! sum is one of the functions in Base that are created using @eval, with a mechanism used to define function interfaces with the same signature but for different functions.

Take a look:

In this snippet, a loop is going over some fixed tuples in a list and creates a function with e.g. sum as the name and some method definitions with various arguments (in this example sum(a::AbstractArray; dims=:) = _sum(a, dims)). The definition in line 370 is just for documentation purposes (sort of like *.h files are used nowadays in C/C++ programming) - notice the docstring above the definition and the lack of a function body.

2 Likes

Thank you for explaining that, now that make more sens to me. Can you also tell me why is one place is sum(A::AbstractArray; dims) and in other sum(A::AbstractArray)?