Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add hasproperty and hasfield to Base RFC #30496

Merged
merged 10 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,9 @@ export
fieldnames,
fieldcount,
fieldtypes,
hasfield,
propertynames,
hasproperty,
isabstracttype,
isbitstype,
isprimitivetype,
Expand Down
16 changes: 16 additions & 0 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ fieldnames(::Core.TypeofBottom) =
throw(ArgumentError("The empty type does not have field names since it does not have instances."))
fieldnames(t::Type{<:Tuple}) = ntuple(identity, fieldcount(t))

"""
hasfield(x, name::Symbol)

Returns a boolean indicating whether the object 'x' has the specified field as one of
its own fields.
"""
hasfield(x, s::Symbol) = hasproperty(x, s)
sam0410 marked this conversation as resolved.
Show resolved Hide resolved

"""
nameof(t::DataType) -> Symbol

Expand Down Expand Up @@ -1203,3 +1211,11 @@ REPL tab completion on `x.` shows only the `private=false` properties.
propertynames(x) = fieldnames(typeof(x))
propertynames(m::Module) = names(m)
propertynames(x, private) = propertynames(x) # ignore private flag by default

"""
hasproperty(x, s::Symbol)

Returns a boolean indicating whether the object `x` has the specified property as one of
its own properties.
"""
hasproperty(x, s::Symbol) = s in propertynames(x)
4 changes: 4 additions & 0 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ mutable struct TLayout
end
tlayout = TLayout(5,7,11)
@test fieldnames(TLayout) == (:x, :y, :z) == Base.propertynames(tlayout)
@test hasfield(TLayout, :y)
@test !hasfield(TLayout, :a)
@test hasproperty(tlayout, :x)
@test !hasproperty(tlayout, :p)
@test [(fieldoffset(TLayout,i), fieldname(TLayout,i), fieldtype(TLayout,i)) for i = 1:fieldcount(TLayout)] ==
[(0, :x, Int8), (2, :y, Int16), (4, :z, Int32)]
@test fieldnames(Complex) === (:re, :im)
Expand Down