Skip to content

Commit 5f945ff

Browse files
committed
Add Compat for TypeUtils
See JuliaLang/julia#20006
1 parent 586d784 commit 5f945ff

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ Currently, the `@compat` macro supports the following syntaxes:
108108

109109
* `Compat.isapprox` with `nans` keyword argument [#20022](https://github.com/JuliaLang/julia/pull/20022)
110110

111+
* The `isabstract`, `parameter_upper_bound`, `typename` reflection methods were added in Julia 0.6. This package re-exports these from the `Compat.TypeUtils`. On earlier versions of julia, that module contains the same functions, but operating on the pre-0.6 type system representation.
112+
111113
## Renamed functions
112114

113115
* `pointer_to_array` and `pointer_to_string` have been replaced with `unsafe_wrap(Array, ...)` and `unsafe_wrap(String, ...)` respectively

src/Compat.jl

+11-1
Original file line numberDiff line numberDiff line change
@@ -1770,4 +1770,14 @@ else
17701770
import Base.isapprox
17711771
end
17721772

1773-
end # module
1773+
module TypeUtils
1774+
@static if isdefined(Core, :UnionAll)
1775+
using Base: isabstract, parameter_upper_bound, typename
1776+
else
1777+
isabstract(t::DataType) = t.abstract
1778+
parameter_upper_bound(t::DataType, idx) = t.parameters[idx].ub
1779+
typename(t::DataType) = t.name
1780+
end
1781+
export isabstract, parameter_upper_bound, typename
1782+
end # module TypeUtils
1783+
end # module Compat

test/runtests.jl

+8
Original file line numberDiff line numberDiff line change
@@ -1603,3 +1603,11 @@ end
16031603
# julia#20022
16041604
@test !Compat.isapprox(NaN, NaN)
16051605
@test Compat.isapprox(NaN, NaN, nans=true)
1606+
1607+
# julia#20006
1608+
abstract AbstractFoo20006
1609+
immutable ConcreteFoo20006{T<:Int} <: AbstractFoo20006
1610+
end
1611+
@test Compat.TypeUtils.isabstract(AbstractFoo20006)
1612+
@test Compat.TypeUtils.parameter_upper_bound(ConcreteFoo20006, 1) == Int
1613+
@test isa(Compat.TypeUtils.typename(Array), TypeName)

0 commit comments

Comments
 (0)