Skip to content
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

Generic fallbacks for type reflection #43253

Open
BeastyBlacksmith opened this issue Nov 29, 2021 · 5 comments
Open

Generic fallbacks for type reflection #43253

BeastyBlacksmith opened this issue Nov 29, 2021 · 5 comments
Labels
docs This change adds or pertains to documentation

Comments

@BeastyBlacksmith
Copy link

I think it would make sense for the reflection functions on types such as isstructtype, isabstracttype, etc. to have the generic fallback

isstructtype(x) = false

For example getting the names of all types defined in a module via

filter(x->(x = getproperty(MODULE, x); isstructtype(x)), names(MODULE, all = true))

currently errors.

I'd happy to make a PR if this would be a worthwhile addition to Base.

@aviatesk
Copy link
Sponsor Member

aviatesk commented Nov 30, 2021

FWIW, isstructtype now doesn't throw MethodError when taking arbitrary type object:

function isstructtype(@nospecialize t)

To me, the essential problem here seems to be that actual method signatures of those reflection tools are sometimes not reflected in documentation. So I guess it would be more appropriated if your PR takes a direction of syncing the actual method signatures with those described in docstrings.

@JeffBezanson
Copy link
Sponsor Member

AFAICT isstructtype already returns false for arbitrary objects; what error are you getting?

julia> isstructtype(false)
false

julia> isstructtype(nothing)
false

julia> isstructtype("")
false

@aviatesk
Copy link
Sponsor Member

aviatesk commented Dec 1, 2021

AFAICT isstructtype already returns false for arbitrary objects

The method signature of isstructtype was widened fairly recently in #43087 in order to make its @nospecialize annotation to be functional (although we also fixed that need in #43113 too).

@JeffBezanson JeffBezanson added the docs This change adds or pertains to documentation label Dec 1, 2021
@BeastyBlacksmith
Copy link
Author

what error are you getting?

At least on 1.7.0 i get

ERROR: MethodError: no method matching isstructtype(::IdDict{Any, Any})
Closest candidates are:
  isstructtype(::Type) at ~/.asdf/installs/julia/1.7.0/share/julia/base/reflection.jl:499
Stacktrace:
 [1] (::var"#5#6")(x::Symbol)
   @ Main ./REPL[5]:1
 [2] filter(f::var"#5#6", a::Vector{Symbol})
   @ Base ./array.jl:2484
 [3] top-level scope
   @ REPL[5]:1

@inkydragon
Copy link
Sponsor Member

v1.7.0:

function isstructtype(@nospecialize(t::Type))

@nospecialize(t::Type) -> @nospecialize t: #43087

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

4 participants