Hi,
I am looking for a way to resolve a module or a field by its full name
I have not found anything in Base (reflection.jl etc.)
Is there anything recommended ? more than my own version there :
field(qname::Tuple{Symbol,Vararg{Symbol}}; root::Module=Main, do_walk_datatype::Bool=false) =
let r = foldl(qname; init=(true,root)) do acc,n
acc[1] || return acc
do_walk_datatype || acc[2] isa Module || return (false,)
e = try getfield(acc[2], n); catch ex; nothing end
e !== nothing || return (false,)
(true, e)
end ;
r[1] || throw(ArgumentError(join(qname,'.')))
r[2]
end
field(name::Symbol; root::Module=Main) = field((name,); root)
using Test
@testset begin
@test field((:Base, :Iterators)) == Base.Iterators
@test field((:Base, :Iterators, :takewhile)) == Base.Iterators.takewhile
@test field((:Iterators, :takewhile); root=Base) == Base.Iterators.takewhile
@test_throws ArgumentError field((:Base, :Iterators, :takewhile2))
@test_throws ArgumentError field((:Base,:stdout,:buffer,:data))
@test field((:Base,:stdout,:buffer,:data); do_walk_datatype=true) isa AbstractVector{UInt8}
@test field(:sin) == field((:Base,:sin))
end
Thanks
–
PS hasfield(::Type,::Symbol)
exists but not hasfield(::Module,::Symbol)
SEE