# Public Reflection API

**URL:** <https://discourse.julialang.org/t/public-reflection-api/114062>\
**Category:** Internals & Design\
**Created:** [May 9, 2024, 5:01pm UTC](https://discourse.julialang.org/t/public-reflection-api/114062 "2024-05-09T17:01:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [May 9, 2024, 5:01pm UTC](https://discourse.julialang.org/t/public-reflection-api/114062/1 "2024-05-09T17:01:12Z")

</div>

@oxinabox, [noted paragon of virtue](https://julialang.slack.com/archives/C67910KEH/p1715273211631269?thread_ts=1715171717.472039&cid=C67910KEH), has done some amazing work for the ecosystem making tools for reflection, but those tools rely on internals.

She has had this issue open for 7 years:

> <https://github.com/JuliaLang/julia/issues/20555>
>
> Julia allows for reflection.
> It is infact really powerful. You can do crazy thi…ngs \[like examine the code for a function programatically\](http://stackoverflow.com/a/39305272/179081).
> 
> My issue is that it is largely based on unexported methods, and on directly reading various fields. Sometimes in complicated ways. Code based on this "privately" facing API breaks every single release.
> 
> Consider, this example that came up in code I am writing.
> I would like to find out if a function has any methods that take a keyword arg with a given name.
> 
> \`\`\`julia
> function methods\_with\_kwarg(func::Function, kwarg\_name::Symbol)
> #BLACK-MAGIC, 0.5 only
> supporting\_methods = Method\[\]
> ml = methods(func)
> if isdefined(ml.mt, :kwsorter)
> kwsort\_t = typeof(ml.mt.kwsorter)
> for mmm in ml.ms
> sig = mmm.sig
> all\_kwarg\_list = Base.kwarg\_decl(sig, kwsort\_t)
> if kwarg\_name ∈ all\_kwarg\_list
> push!(supporting\_methods, mmm)
> end
> end
> end
> supporting\_methods
> end
> \`\`\`
> 
> \`\`\`julia
> julia\> methods\_with\_kwarg(repeat, :outer)
> 
> 1-element Array{Method,1}:
> - repeat(A::AbstractArray) at abstractarraymath.jl:329
> \`\`\`
> 
> So in that code, 2 Public API method was used: \`methods\` and \`isdefined\`.
> And then 5 private API methods/fields: \`MethodTable.kwsorter\`, \`Method.sig\`, \`MethodList.ms\`, \`MethodList.mt\`, and \`Base.kwarg\_decl\`.
> 
> They are all undocumented. and I know they change in 0.6, and in 0.4, and 0.3.
> It would be nice to be able to do reflection without having to go deep into the guts of the language.
> 
> Towards this end, I feel it is necessary to make a consideration of what reflection methods a language like julia should provide, and then define, export, and document methods to that end.

Does anyone have a sense of if this has languished due to disagreement on the API, disinterest in exposing an API at all, or just time to implement a solution?

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [May 10, 2024, 3:51pm UTC](https://discourse.julialang.org/t/public-reflection-api/114062/2 "2024-05-10T15:51:05Z")

</div>

Opened a small PR to take a swing at this:

> <https://github.com/JuliaLang/julia/pull/54432>
>
> This PR documents \`Method\`, \`MethodList\`, and a few functions for retrieving the… properties of a \`Method\`. \`methods\` is partially documented but isn't very useful outside of interactive sessions because nothing it returns is public. The docs are also currently incorrect because they claim that \`methods\` returns a \`MethodTable\`, but it actually returns a \`MethodList\`. 
> 
> This partially addresses #20555, but I am intentionally keeping the surface area of this change very small so to judge the interest in this direction. To that end, I do not document \`MethodTable\`, anything to do with kwargs, nor do I define functions to access any of the more "advanced" properties of \`Method\`.
> 
> This is my first time contributing, so I don't have tooling set up to test building the docs. There is still a lot of clean up to do with regard to adding tests for the new functions, but I want an initial reaction from the maintainers about this PR before burning more time.
