@@ -118,6 +118,35 @@ Union type of [`DenseVector{T}`](@ref) and [`DenseMatrix{T}`](@ref).
118118"""
119119const DenseVecOrMat{T} = Union{DenseVector{T}, DenseMatrix{T}}
120120
121+ """
122+ ImmutableArray
123+
124+ Dynamically allocated, immutable array.
125+
126+ """
127+ const ImmutableArray = Core. ImmutableArray
128+
129+ """
130+ IMArray{T,N}
131+
132+ Union type of [`Array{T,N}`](@ref) and [`ImmutableArray{T,N}`](@ref)
133+ """
134+ const IMArray{T,N} = Union{Array{T, N}, ImmutableArray{T,N}}
135+
136+ """
137+ IMVector{T}
138+
139+ One-dimensional [`ImmutableArray`](@ref) or [`Array`](@ref) with elements of type `T`. Alias for `IMArray{T, 1}`.
140+ """
141+ const IMVector{T} = IMArray{T, 1 }
142+
143+ """
144+ IMMatrix{T}
145+
146+ Two-dimensional [`ImmutableArray`](@ref) or [`Array`](@ref) with elements of type `T`. Alias for `IMArray{T,2}`.
147+ """
148+ const IMMatrix{T} = IMArray{T, 2 }
149+
121150# # Basic functions ##
122151
123152import Core: arraysize, arrayset, arrayref, const_arrayref
@@ -147,14 +176,13 @@ function vect(X...)
147176 return copyto! (Vector {T} (undef, length (X)), X)
148177end
149178
150- const ImmutableArray = Core. ImmutableArray
151- const IMArray{T,N} = Union{Array{T, N}, ImmutableArray{T,N}}
152- const IMVector{T} = IMArray{T, 1 }
153- const IMMatrix{T} = IMArray{T, 2 }
154-
179+ # Freeze and thaw constructors
155180ImmutableArray (a:: Array ) = Core. arrayfreeze (a)
156181Array (a:: ImmutableArray ) = Core. arraythaw (a)
157182
183+ ImmutableArray (a:: AbstractArray{T,N} ) where {T,N} = ImmutableArray {T,N} (a)
184+
185+ # Size functions for arrays, both mutable and immutable
158186size (a:: IMArray , d:: Integer ) = arraysize (a, convert (Int, d))
159187size (a:: IMVector ) = (arraysize (a,1 ),)
160188size (a:: IMMatrix ) = (arraysize (a,1 ), arraysize (a,2 ))
@@ -393,6 +421,9 @@ similar(a::Array{T}, m::Int) where {T} = Vector{T}(undef, m)
393421similar (a:: Array , T:: Type , dims:: Dims{N} ) where {N} = Array {T,N} (undef, dims)
394422similar (a:: Array{T} , dims:: Dims{N} ) where {T,N} = Array {T,N} (undef, dims)
395423
424+ ImmutableArray {T} (undef:: UndefInitializer , m:: Int ) where T = ImmutableArray (Array {T} (undef, m))
425+ ImmutableArray {T} (undef:: UndefInitializer , dims:: Dims ) where T = ImmutableArray (Array {T} (undef, dims))
426+
396427# T[x...] constructs Array{T,1}
397428"""
398429 getindex(type[, elements...])
@@ -626,8 +657,8 @@ oneunit(x::AbstractMatrix{T}) where {T} = _one(oneunit(T), x)
626657
627658# # Conversions ##
628659
629- convert (:: Type{T} , a:: AbstractArray ) where {T<: Array } = a isa T ? a : T (a)
630660convert (:: Type{Union{}} , a:: AbstractArray ) = throw (MethodError (convert, (Union{}, a)))
661+ convert (T:: Union{Type{<:Array},Type{<:Core.ImmutableArray}} , a:: AbstractArray ) = a isa T ? a : T (a)
631662
632663promote_rule (a:: Type{Array{T,n}} , b:: Type{Array{S,n}} ) where {T,n,S} = el_same (promote_type (T,S), a, b)
633664
@@ -637,6 +668,7 @@ if nameof(@__MODULE__) === :Base # avoid method overwrite
637668# constructors should make copies
638669Array {T,N} (x:: AbstractArray{S,N} ) where {T,N,S} = copyto_axcheck! (Array {T,N} (undef, size (x)), x)
639670AbstractArray {T,N} (A:: AbstractArray{S,N} ) where {T,N,S} = copyto_axcheck! (similar (A,T), A)
671+ ImmutableArray {T,N} (Ar:: AbstractArray{S,N} ) where {T,N,S} = Core. arrayfreeze (copyto_axcheck! (Array {T,N} (undef, size (Ar)), Ar))
640672end
641673
642674# # copying iterators to containers
0 commit comments