Skip to content

Commit 24b5aa8

Browse files
committed
Add a value-version of of_eltyp
Especially in anonymous functions, value-arguments are easier to keep type-stable than types-as-arguments
1 parent 1d62d52 commit 24b5aa8

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/MappedArrays.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ end
3838

3939
"""
4040
of_eltype(T, A)
41+
of_eltype(val::T, A)
4142
4243
creates a view of `A` that lazily-converts the element type to `T`.
4344
"""
4445
of_eltype{S,T}(::Type{T}, data::AbstractArray{S}) = mappedarray((x->convert(T,x), y->convert(S,y)), data)
4546
of_eltype{T}(::Type{T}, data::AbstractArray{T}) = data
47+
of_eltype{S,T}(::T, data::AbstractArray{S}) = of_eltype(T, data)
4648

4749
Base.parent(A::AbstractMappedArray) = A.data
4850
Base.size(A::AbstractMappedArray) = size(A.data)

test/runtests.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ b[2,1] = 10/255
4848
a = [0.1 0.3; 0.2 0.4]
4949
b = @inferred(of_eltype(UFixed8, a))
5050
@test b[1,1] === UFixed8(0.1)
51+
b = @inferred(of_eltype(zero(UFixed8), a))
52+
@test b[1,1] === UFixed8(0.1)
5153
b[2,1] = UFixed8(0.5)
5254
@test a[2,1] == UFixed8(0.5)
5355
@test !(b === a)
54-
b = of_eltype(Float64, a)
56+
b = @inferred(of_eltype(Float64, a))
57+
@test b === a
58+
b = @inferred(of_eltype(0.0, a))
5559
@test b === a

0 commit comments

Comments
 (0)