Unexpected output for @which with keyword constructor

The output for @which Hammer(weight = 25) is not what I expected, nor is it consistent with the other invocations. Not sure if bug, known issue, or by design:

abstract type Product end

abstract type Tool <: Product end

struct Hammer <: Tool
       weight::Int
end

function Hammer(weight = 10)
       Hammer(weight)
end

function Hammer(; weight = 10)
       Hammer(weight)
end

@which Hammer(30)
Hammer(weight::Int64) # in Main at REPL[5]:2

@which Hammer()
Hammer() # in Main at REPL[53]:2

@which Hammer(weight = 25)
(::getfield(Core, Symbol("#kw#Type")))(::Any, ::Type{Hammer}) # in Main 😲

That’s just how keyword arguments are implemented – Julia creates a wrapper function:

julia> f(;x) = x
f (generic function with 1 method)

julia> @which f(x=3)
(::getfield(Main, Symbol("#kw##f")))(::Any, ::typeof(f)) in Main

IMHO @which shouldn’t try to be smart here and show you the method you defined, or anything like that.

1 Like

I see, thank you.

Can’t comment as I don’t know how difficult would be to make @which smarter. However, this design exposes the internals of the implementation, which by itself is not useful nor desirable. It doesn’t really serve the purpose of @which - how is that useful to a user?

I stumbled into this while working on some code showcasing the usage of @which and this left my explanation hanging in mid-air.

Not a big deal but maybe it’s worth fixing, if not too difficult.