diff --git a/NEWS.md b/NEWS.md index 37df2ee..20667a8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,16 @@ # User visible changes in `ArrayTools` package +## Version 0.3.0 + +- Use package `TypeUtils` and re-export some of its methods: `as`, `as_eltype`, + `convert_eltype`, and `promote_eltype`. + +- Method `to_type(T,x)` has been deprecated in favor of `as(T,x)` found in + package `TypeUtils`. + +- `promote_eltype()` with no arguments returns the same result as + `promote_type()` and `UndefinedType` has been deleted. + ## Version 0.2.7 - Rename `@assert_same_indices` as `@assert_same_axes` which is more specific @@ -38,7 +49,6 @@ - Some code ahs been simplified. - ## Version 0.2.1 - `isfastarray` and `isflatarray` deprecated in favor of `is_fast_array` and diff --git a/Project.toml b/Project.toml index 4d0a258..09efff1 100644 --- a/Project.toml +++ b/Project.toml @@ -1,14 +1,18 @@ name = "ArrayTools" uuid = "1dc0ca97-c5ce-4e77-ac6d-c576ac9d7f27" authors = ["Éric Thiébaut "] -version = "0.2.7" +version = "0.3.0" + +[deps] +TypeUtils = "c3b1956e-8857-4d84-9b79-890df85b1e67" [compat] +TypeUtils = "≥0.2.2" julia = "1.0" [extras] -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] test = ["Test", "Random"] diff --git a/src/ArrayTools.jl b/src/ArrayTools.jl index 40eb54a..5095e40 100644 --- a/src/ArrayTools.jl +++ b/src/ArrayTools.jl @@ -8,7 +8,6 @@ export MaybeArrayAxes, MaybeArrayAxis, RubberIndex, - UndefinedType, all_match, allof, anyof, @@ -22,7 +21,6 @@ export common_indices, has_standard_indexing, noneof, - promote_eltype, reversemap, all_indices, same_axes, @@ -35,25 +33,38 @@ export to_axis, to_int, to_size, - to_type, - # storage trait + # to_type, # FIXME: deprecated, use `as(T,x)` instead + + # Re-exports from TypeUtils: + as, + as_eltype, + convert_eltype, + promote_eltype, + + # Storage trait: StorageType, AnyStorage, FlatStorage, to_flat_array, is_flat_array, + # Fast arrays and indexing trait: IndexingType, FastIndexing, AnyIndexing, to_fast_array, is_fast_array, + # Macros: @assert_same_axes +using TypeUtils + using Base: OneTo, axes1, tail import Base: dotview, getindex, setindex!, to_indices +@deprecate to_type(T, x) as(T, x) true + include("traits.jl") include("utils.jl") include("indexing.jl") diff --git a/src/indexing.jl b/src/indexing.jl index 7b347c1..78f0885 100644 --- a/src/indexing.jl +++ b/src/indexing.jl @@ -111,9 +111,9 @@ sub-type of [`ArrayAxis`](@ref) which is an alias to `AbstractUnitRange{Int}`. """ to_axis(ind::ArrayAxis) = ind -to_axis(ind::AbstractUnitRange{<:Integer}) = to_type(ArrayAxis, ind) +to_axis(ind::AbstractUnitRange{<:Integer}) = as(ArrayAxis, ind) to_axis(ind::Int) = Base.OneTo(ind) -to_axis(ind::Integer) = to_axis(to_type(Int, ind)) +to_axis(ind::Integer) = to_axis(as(Int, ind)) """ ArrayAxes{N} diff --git a/src/utils.jl b/src/utils.jl index 95ca790..896d393 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -4,15 +4,6 @@ # General purpose methods. # -""" - to_type(T, x) - -yields `x` converted to type `T`, the result is asserted to be of type `T`. - -""" -to_type(::Type{T}, x::T) where {T} = x -to_type(::Type{T}, x::Any) where {T} = convert(T, x)::T - """ to_int(x) @@ -21,7 +12,7 @@ converts `x` to a similar object whose values are all of type `Int`. Argument already of the correct type. """ -to_int(x::Integer) = to_type(Int, x) +to_int(x::Integer) = as(Int, x) to_int(I::AbstractUnitRange{Int}) = I to_int(I::AbstractUnitRange{<:Integer}) = to_int(first(I)):to_int(last(I)) to_int(I::Base.OneTo{Int}) = I @@ -34,30 +25,6 @@ to_int(A::AbstractArray{<:Integer,N}) where {N} = map(to_int, A) to_int(x::Tuple{Vararg{Int}}) = x to_int(x::Tuple{Vararg{Integer}}) = map(to_int, x) -""" - UndefinedType - -is a type used to represent undefined type in `promote_type`. It is an -abstract type so that `isbitstype(UndefinedType)` is false. - -""" -abstract type UndefinedType end - -""" - promote_eltype(args...) - -yields the promoted element type of its arguments. Arguments `args...` may be -anything implementing the `eltype` method. - -""" -promote_eltype(arg) = eltype(arg) -promote_eltype(args...) = promote_type(map(eltype, args)...) -promote_eltype() = UndefinedType - -Base.promote_type(T::Type, ::Type{UndefinedType}) = T -Base.promote_type(::Type{UndefinedType}, T::Type) = T -Base.promote_type(::Type{UndefinedType}, ::Type{UndefinedType}) = UndefinedType - """ all_match(val, f, args...) -> bool diff --git a/test/runtests.jl b/test/runtests.jl index 1fc4be7..389a311 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -59,7 +59,7 @@ atol = 1e-6 @testset "Miscellaneous " begin # Promotion of element types. for (T1,T2,T3) in ((Float32,Float64,Int), (Int16,Int32,Int64)) - @test promote_eltype() === UndefinedType + @test promote_eltype() === promote_type() @test promote_eltype(zeros(T1,1)) == promote_type(T1) @test promote_eltype(AbstractVector{T2}) == promote_type(T2) @test promote_eltype(zeros(T2,4), zeros(T3,2,3)) == promote_type(T2,T3) @@ -70,15 +70,13 @@ atol = 1e-6 promote_type(T1,T2,T3) @test promote_eltype(DenseMatrix{T1}, zeros(T2,5), AbstractVector{T3}) == promote_type(T1,T2,T3) - @test promote_type(UndefinedType, Int) === Int - @test promote_type(Bool, UndefinedType) === Bool - @test promote_type(UndefinedType, UndefinedType) === UndefinedType end # Conversions. - @test to_type(Int32, 3) === Int32(3) - @test to_type(Float32, 3) === Float32(3) - @test to_type(Vector{Float32}, [1,2,3]) == Float32[1,2,3] - @test isa(to_type(Vector{Float32}, [1,2,3]), Vector{Float32}) + @test_deprecated to_type(Int32, 3) + @test as(Int32, 3) === Int32(3) + @test as(Float32, 3) === Float32(3) + @test as(Vector{Float32}, [1,2,3]) == Float32[1,2,3] + @test as(Vector{Float32}, [1,2,3]) isa Vector{Float32} @test to_int(2) === 2 @test to_int(0x03) === 3 @test to_int(Base.OneTo(5)) === Base.OneTo(5)