# \`methods\` and \`@which\` give different results

**URL:** <https://discourse.julialang.org/t/methods-and-which-give-different-results/127954>\
**Category:** General Usage\
**Tags:** kwargs, methods, keyword-arguments, specializations\
**Created:** [April 11, 2025, 1:50am UTC](https://discourse.julialang.org/t/methods-and-which-give-different-results/127954 "2025-04-11T01:50:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)\
**Post date:** [April 11, 2025, 1:50am UTC](https://discourse.julialang.org/t/methods-and-which-give-different-results/127954/1 "2025-04-11T01:50:14Z")

</div>

I have:

```julia
f(; x) = 0

```

and I do:

```julia
f(x = 1)

```

creating one specialized method (MethodInstance?) of `f`.

I can see the specialized method with a `@which`:

```julia
julia> (@which f(x = 1)) |> Base.specializations
Base.MethodSpecializations(MethodInstance for Core.kwcall(::@NamedTuple{x::Int64}, ::typeof(f)))

```

But using `methods` shows me nothing:

```julia
julia> f |> methods |> only |> Base.specializations
Base.MethodSpecializations(svec())

```

`@which` is showing me a method of `kwcall`:

```julia
julia> (@which f(x = 1))
kwcall(::NamedTuple, ::typeof(f))

```

but `methods` shows me something else:

```julia
julia> methods(f) |> only
f(; x)

```

This does not hold:

```julia
(@which g(1)) == only(methods(g))

```

How can I reliably get all methods of a function and each method’s specializations?

---

<div class="post-metadata">

**Author:** ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)\
**Post date:** [April 11, 2025, 1:54am UTC](https://discourse.julialang.org/t/methods-and-which-give-different-results/127954/2 "2025-04-11T01:54:21Z")

</div>

This seems specific to functions with keyword arguments. If I have:

```julia
g(x) = 0
g(1)

```

This holds:

```julia
(@which g(1)) == only(methods(g))

```

---

<div class="post-metadata">

**Author:** ![Liozou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liozou/32/35118_2.png) [@Liozou](https://discourse.julialang.org/u/Liozou)\
**Post date:** [April 11, 2025, 2:43pm UTC](https://discourse.julialang.org/t/methods-and-which-give-different-results/127954/3 "2025-04-11T14:43:52Z")

</div>

The method which is called for `f(; x=1)` is `Core.kwcall(...)` as correctly given by `@which`. However, that method is not a method of function `f` but a method of function `Core.kwcall`, as indicated by its name, so it won’t appear in `methods(f)`. Actually, the only method that appears in `methods(f)` is that which is called with `f()`, which throws an `UndefKeywordError` error: you can indeed check that:

```julia
julia> (@which f()) == only(methods(f))
true

```

The full details of the inner workings of keywods arguments are given in the dev docs: [Julia Functions · The Julia Language](https://docs.julialang.org/en/v1/devdocs/functions/#Keyword-arguments)  
and as you can see there, whenever a method with keyword arguments is defined in the code, multiple methods are actually defined, so if you wanted to collect all the relevant methods for your example case here, there would be the one in `methods(f)`, the `Core.kwcall` one you can get from `@which`, and a last one found as `methods(var"#f#1")`… however the number at the end (here `1`) depends on how many keyword argument methods were defined before, and it is an implementation detail of the compiler, I don’t know if there is a proper way to retrieve that name `var"#f#1"`.

---

<div class="post-metadata">

**Author:** ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)\
**Post date:** [April 11, 2025, 2:59pm UTC](https://discourse.julialang.org/t/methods-and-which-give-different-results/127954/4 "2025-04-11T14:59:19Z")

</div>

I’m taking that to mean that there’s no reliable way to get a list of all method specializations.

Also, to make things more confusing, the `kwcall` method (correctly?) has the name `:f`.

```julia
(@which f(x = 1)).name == :f

```

Or maybe this helps, actually. I could get a list of all `kwcall` methods with name `:f`.

---

<div class="post-metadata">

**Author:** ![Liozou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liozou/32/35118_2.png) [@Liozou](https://discourse.julialang.org/u/Liozou)\
**Post date:** [April 11, 2025, 3:12pm UTC](https://discourse.julialang.org/t/methods-and-which-give-different-results/127954/5 "2025-04-11T15:12:45Z")

</div>

It’s not exactly a matter of finding the method specializations but rather finding the method itself. But even though I can’t really recommend it as part of a workflow, you can always do:

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

julia> f(y; x1, x2) = y*(x1 + 2x2)
f (generic function with 2 methods)

julia> kwfunctions = eval.(filter(x -> startswith(String(x), "#f#"), names(Main; all=true, imported=false)))
2-element Vector{Function}:
 #f#1 (generic function with 1 method)
 #f#2 (generic function with 1 method)

julia> [Base.specializations(only(methods(x))) for x in kwfunctions]
2-element Vector{Base.MethodSpecializations}:
 Base.MethodSpecializations(svec())
 Base.MethodSpecializations(svec())

julia> f(; x=36)
72

julia> [Base.specializations(only(methods(x))) for x in kwfunctions]
2-element Vector{Base.MethodSpecializations}:
 Base.MethodSpecializations(MethodInstance for var"#f#1"(::Int64, ::typeof(f)))
 Base.MethodSpecializations(svec())

julia> f(14; x1=13.6, x2=4//5)
212.79999999999998

julia> [Base.specializations(only(methods(x))) for x in kwfunctions]
2-element Vector{Base.MethodSpecializations}:
 Base.MethodSpecializations(MethodInstance for var"#f#1"(::Int64, ::typeof(f)))
 Base.MethodSpecializations(MethodInstance for var"#f#2"(::Float64, ::Rational{Int64}, ::typeof(f), ::Int64))

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [April 11, 2025, 5:25pm UTC](https://discourse.julialang.org/t/methods-and-which-give-different-results/127954/6 "2025-04-11T17:25:29Z")

</div>

> [@sadish-d](#):
>
> there’s no reliable way to get a list of all method specializations.

Method specializations are internal details of the language’s implementation, which are subject to changes across versions and aren’t part of the language’s standard. If you make base language API that “gets a method’s specializations”, you effectively force method specializations into the language standard and limit the implementations. This sort of reflection will at best be an unstable, implementation-specific, third-party library.

---

<div class="post-metadata">

**Author:** ![Liozou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liozou/32/35118_2.png) [@Liozou](https://discourse.julialang.org/u/Liozou)\
**Post date:** [April 11, 2025, 5:36pm UTC](https://discourse.julialang.org/t/methods-and-which-give-different-results/127954/7 "2025-04-11T17:36:06Z")

</div>

> [@Benny](#):
>
> aren’t part of the language’s standard

That’s right, and actually even `Base.specializations` is not part of the public API, as you can see from the REPL documentation:

```julia
help?> Base.specializations
  │ Warning
  │
  │ The following bindings may be internal; they may change or be removed in future versions:
  │
  │ • Base.specializations

```

I find this kind of question very interesting to learn how the internals of the language work, but you can’t rely on the hack I showed before (or even `Base.specializations` thus) for production purpose.

---

<div class="post-metadata">

**Author:** ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)\
**Post date:** [April 11, 2025, 5:46pm UTC](https://discourse.julialang.org/t/methods-and-which-give-different-results/127954/8 "2025-04-11T17:46:29Z")

</div>

I appreciate the caution that I’m venturing into non-public API. I was just trying to learn about methods and dispatch vs specialization, and am not relying on these internals for production.
