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

Add hasfield and hasproperty #650

Merged
merged 3 commits into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ Currently, the `@compat` macro supports the following syntaxes:

* `mapslices` with `dims` keyword argument ([#27828]).

* `hasproperty` and `hasfield` ([#28850]).
`hasproperty` is defined only for Julia 0.7 or later.

## Renaming

* `Display` is now `AbstractDisplay` ([#24831]).
Expand Down Expand Up @@ -577,3 +580,4 @@ includes this fix. Find the minimum version from there.
[#28302]: https://github.com/JuliaLang/julia/issues/28302
[#28303]: https://github.com/JuliaLang/julia/issues/28303
[#28708]: https://github.com/JuliaLang/julia/issues/28708
[#28850]: https://github.com/JuliaLang/julia/issues/28850
11 changes: 11 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1891,6 +1891,17 @@ else
range(start::Number, stop::Number; kwargs...) = range(start; stop=stop, kwargs...)
end

# https://github.com/JuliaLang/julia/pull/30496
if VERSION < v"1.2.0-DEV.272"
Base.@pure hasfield(::Type{T}, name::Symbol) where T =
Base.fieldindex(T, name, false) > 0
export hasfield
if VERSION >= v"0.7-"
tkf marked this conversation as resolved.
Show resolved Hide resolved
hasproperty(x, s::Symbol) = s in propertynames(x)
export hasproperty
end
end

include("deprecated.jl")

end # module Compat
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1532,4 +1532,17 @@ end
@test Compat.range(0, 5, length = 6) == 0.0:1.0:5.0
@test Compat.range(0, 10, step = 2) == 0:2:10

mutable struct TLayout
x::Int8
y::Int16
z::Int32
end
tlayout = TLayout(5,7,11)
@test hasfield(TLayout, :y)
@test !hasfield(TLayout, :a)
if VERSION >= v"0.7-"
@test hasproperty(tlayout, :x)
@test !hasproperty(tlayout, :p)
end

nothing