Skip to content

Commit b1098f4

Browse files
authored
Merge pull request #319 from JuliaLang/teh/kf/typeutils
Add Compat for TypeUtils: fix for julia 0.4
2 parents 6b6dfc9 + f598c27 commit b1098f4

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ Currently, the `@compat` macro supports the following syntaxes:
110110

111111
* `take!` method for `Task`s since some functions now return `Channel`s instead of `Task`s [#19841](https://github.com/JuliaLang/julia/pull/19841).
112112

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

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

src/Compat.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1773,4 +1773,15 @@ else
17731773
import Base.isapprox
17741774
end
17751775

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

test/runtests.jl

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

0 commit comments

Comments
 (0)