-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document Method
reflection functions
#54432
base: master
Are you sure you want to change the base?
Changes from 10 commits
83461ce
40e28dc
934f05d
7955d0b
405e0c6
2ced667
320d948
b1ddd99
2955539
4069be4
6706d6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,8 @@ public | |
isexported, | ||
ispublic, | ||
remove_linenums!, | ||
method_argnames, | ||
signature_type, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do other folks think about making |
||
|
||
# Opperators | ||
operator_associativity, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1123,6 +1123,30 @@ function signature_type(@nospecialize(f), @nospecialize(argtypes)) | |
return rewrap_unionall(Tuple{ft, u.parameters...}, argtypes) | ||
end | ||
|
||
""" | ||
signature_type(m::Method) -> Type{<:Tuple} | ||
|
||
Retrieve the signature type of a `Method`. Returns a `Tuple` type, the first element of which is | ||
the `typeof` the function for the method. The remaining elements are the types of the arguments. | ||
|
||
For methods that are `OpaqueClosure`s or `Builtin`s, `Tuple{}` is returned. However, this | ||
behavior is not stable because future improvements to the compiler may allow improved reflection for | ||
these types. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> f(x::Int, y) = x + y | ||
|
||
julia> m = methods(f)[1] | ||
|
||
julia> signature_type(m) | ||
Tuple{typeof(f), Int, Any} | ||
``` | ||
""" | ||
function signature_type(m::Method) | ||
return m.sig | ||
end | ||
|
||
""" | ||
code_lowered(f, types; generated=true, debuginfo=:default) | ||
|
||
|
@@ -1198,14 +1222,18 @@ end | |
|
||
# high-level, more convenient method lookup functions | ||
|
||
# type for reflecting and pretty-printing a subset of methods | ||
""" | ||
`MethodList` is a type for reflecting and pretty-printing a subset of methods. It is returned by | ||
[`methods`](@ref). | ||
""" | ||
mutable struct MethodList <: AbstractArray{Method,1} | ||
ms::Array{Method,1} | ||
mt::Core.MethodTable | ||
end | ||
|
||
size(m::MethodList) = size(m.ms) | ||
getindex(m::MethodList, i::Integer) = m.ms[i] | ||
collect(m::MethodList) = m.ms | ||
mrufsvold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function MethodList(mt::Core.MethodTable) | ||
ms = Method[] | ||
|
@@ -1216,9 +1244,10 @@ function MethodList(mt::Core.MethodTable) | |
end | ||
|
||
""" | ||
methods(f, [types], [module]) | ||
methods(f, [types], [module]) -> AbstractVector{Method} | ||
|
||
Return the method table for `f`. | ||
Return a list of [`Method`](@ref)s for `f`. The returned container type is not specified. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CI fails because this cross-ref is broken. Probably because the |
||
Use `collect(methods(f))` to construct a `Vector{Method}`. | ||
|
||
If `types` is specified, return an array of methods whose types match. | ||
If `module` is specified, return an array of methods defined in that module. | ||
|
@@ -2314,6 +2343,15 @@ function nameof(f::Core.IntrinsicFunction) | |
return ccall(:jl_symbol, Ref{Symbol}, (Ptr{UInt8},), name) | ||
end | ||
|
||
""" | ||
nameof(f::Method) -> Symbol | ||
|
||
Get the name of a `Method` as a symbol. | ||
""" | ||
function nameof(m::Method) | ||
return m.name | ||
end | ||
|
||
""" | ||
parentmodule(f::Function) -> Module | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a weird API to make public: all the declared argument names with
#self
prepended.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair about the
#self
piece, but it would be really nice to be able to get the arg names.Do you think cut it completely or define a new function that could provide just the arg names (But then there's the issue of functions defined for callable structs where you actually do want the function "self"...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps replace
Symbol("#self#")
andSymbol("#unused#")
withnothing
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll give it a go!