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
Changes from 2 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
8 changes: 4 additions & 4 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ fieldnames(::Core.TypeofBottom) =
fieldnames(t::Type{<:Tuple}) = ntuple(identity, fieldcount(t))

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

Returns a boolean indicating whether the DataType has the specified field as one of
Returns a boolean indicating whether the object 'x' has the specified field as one of
its own fields.
"""
hasfield(T::DataType, name::Symbol) = fieldindex(T, name, false) > 0
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 @@ -1218,4 +1218,4 @@ propertynames(x, private) = propertynames(x) # ignore private flag by default
Returns a boolean indicating whether the object `x` has the specified property as one of
its own properties.
"""
hasproperty(x, s::Symbol) = isdefined(x, s)
hasproperty(x, s::Symbol) = s in propertynames(x)