Skip to content

Commit 5a6e165

Browse files
authored
introduce "boxing" constructors in place of converting ones (#12)
Do it like `Fill` from FillArrays.jl does it, instead of like `Array` does it. This also makes `wrap_in_0dim` redundant, so delete it.
1 parent da8b880 commit 5a6e165

File tree

3 files changed

+35
-31
lines changed

3 files changed

+35
-31
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ The motivation for creating this package is:
3131
* `ZeroDimensionalArrayMutable` can be a good replacement. Examples:
3232
* make a `const` binding that's mutable:
3333
```julia
34-
const some_const_binding = ZeroDimensionalArrayMutable(fill(0.2))
34+
const some_const_binding = ZeroDimensionalArrayMutable(0.2)
3535
```
3636
* make a field within an immutable `struct` mutable (warning: it's usually more efficient to change the entire `struct` into a `mutable struct`)
3737
```julia
@@ -45,11 +45,11 @@ The motivation for creating this package is:
4545
* https://github.com/JuliaLang/julia/issues/40369
4646
* https://discourse.julialang.org/t/dynamic-immutable-type/127168
4747
* to provide a wrapper type for treating a value as a scalar in broadcasting, something `Ref` is often used for:
48-
* `ZeroDimensionalArrayImmutable` can be a good replacement. Construct one from its only element using the `wrap_in_0dim` function. Like this:
48+
* `ZeroDimensionalArrayImmutable` can be a good replacement:
4949
```julia-repl
5050
julia> using ZeroDimensionalArrays
5151
52-
julia> isa.(wrap_in_0dim([1,2,3]), [Array, Dict, Int])
52+
julia> isa.(ZeroDimensionalArrayImmutable([1,2,3]), [Array, Dict, Int])
5353
3-element BitVector:
5454
1
5555
0

src/ZeroDimensionalArrays.jl

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module ZeroDimensionalArrays
22

33
export
4-
wrap_in_0dim,
54
ZeroDimensionalArrayImmutable,
65
ZeroDimensionalArrayMutable,
76
ZeroDimensionalArrayMutableConstField
@@ -30,10 +29,6 @@ mutable struct ZeroDimensionalArrayMutableConstField{T} <: AbstractArray{T, 0}
3029
end
3130
end
3231

33-
function wrap_in_0dim(v)
34-
new_zero_dimensional_array_immutable(typeof(v), v)
35-
end
36-
3732
const ZeroDimensionalArray = Union{
3833
ZeroDimensionalArrayImmutable,
3934
ZeroDimensionalArrayMutable,
@@ -84,12 +79,21 @@ function Base.only(a::ZeroDimensionalArray)
8479
a[]
8580
end
8681

87-
function convert_from_other_array_to_given_eltype(::Type{Arr}, ::Type{T}, a::AbstractArray{<:Any, 0}) where {Arr <: ZeroDimensionalArray, T}
88-
v = a[]
82+
function construct_given_eltype(::Type{Arr}, ::Type{T}, v) where {Arr <: ZeroDimensionalArray, T}
8983
c = type_to_constructor_function(Arr)
9084
c(T, v)
9185
end
9286

87+
function construct(::Type{Arr}, v) where {Arr <: ZeroDimensionalArray}
88+
T = typeof(v)
89+
construct_given_eltype(Arr, T, v)
90+
end
91+
92+
function convert_from_other_array_to_given_eltype(::Type{Arr}, ::Type{T}, a::AbstractArray{<:Any, 0}) where {Arr <: ZeroDimensionalArray, T}
93+
v = a[]
94+
construct_given_eltype(Arr, T, v)
95+
end
96+
9397
function convert_from_other_array(::Type{Arr}, a::AbstractArray{<:Any, 0}) where {Arr <: ZeroDimensionalArray}
9498
T = eltype(a)
9599
convert_from_other_array_to_given_eltype(Arr, T, a)
@@ -107,11 +111,11 @@ for Arr ∈ (
107111
function Base.convert(::Type{$Arr{T}}, a::AbstractArray{<:Any, 0}) where {T}
108112
convert_from_other_array_to_given_eltype($Arr, T, a)
109113
end
110-
function (::Type{$Arr})(a::AbstractArray{<:Any, 0})
111-
convert_from_other_array($Arr, a)
114+
function (::Type{$Arr})(v)
115+
construct($Arr, v)
112116
end
113-
function (::Type{$Arr{T}})(a::AbstractArray{<:Any, 0}) where {T}
114-
convert_from_other_array_to_given_eltype($Arr, T, a)
117+
function (::Type{$Arr{T}})(v) where {T}
118+
construct_given_eltype($Arr, T, v)
115119
end
116120
end
117121
end

test/runtests.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,26 @@ using Aqua: Aqua
1313
ZeroDimensionalArrayMutable,
1414
ZeroDimensionalArrayMutableConstField,
1515
)
16+
@test (@inferred Arr(0.3)) == (@inferred convert(Arr, fill(0.3)))
1617
@test isstructtype(Arr)
1718
@test Arr <: AbstractArray{<:Any, 0}
18-
@test (@inferred Arr(fill(0.3))) isa Arr{Float64}
19+
@test (@inferred Arr(0.3)) isa Arr{Float64}
1920
@test (@inferred convert(Arr, fill(0.3))) isa Arr{Float64}
20-
@test (@inferred Arr{Float32}(fill(0.3))) isa Arr{Float32}
21+
@test (@inferred Arr{Float32}(0.3)) isa Arr{Float32}
2122
@test (@inferred convert(Arr{Float32}, fill(0.3))) isa Arr{Float32}
22-
@test () === @inferred propertynames(Arr(fill(0.3)))
23-
@test only(fill(0.3)) === @inferred only(Arr(fill(0.3)))
24-
@test fill(0.3)[] === @inferred Arr(fill(0.3))[]
25-
@test fill(0.3)[1] === @inferred Arr(fill(0.3))[1]
26-
@test @inferred isassigned(Arr(fill(0.3)))
27-
@test @inferred isassigned(Arr(fill(0.3)), 1)
28-
@test !(isassigned(Arr(fill(0.3)), 2))
29-
@test (@inferred similar(Arr(fill(0.3)))) isa ZeroDimensionalArrayMutable{Float64}
30-
@test (@inferred similar(Arr(fill(0.3)), Float32)) isa ZeroDimensionalArrayMutable{Float32}
31-
@test fill(0.3) == Arr(fill(0.3))
32-
@test Arr(fill(0.3)) == Arr(fill(0.3))
33-
@test all(@inferred Arr(fill(0.3)) .== Arr(fill(0.3)))
34-
@test (@inferred Arr(fill(0.3)) .+ [10, 20]) isa AbstractVector
23+
@test () === @inferred propertynames(Arr(0.3))
24+
@test only(fill(0.3)) === @inferred only(Arr(0.3))
25+
@test fill(0.3)[] === @inferred Arr(0.3)[]
26+
@test fill(0.3)[1] === @inferred Arr(0.3)[1]
27+
@test @inferred isassigned(Arr(0.3))
28+
@test @inferred isassigned(Arr(0.3), 1)
29+
@test !(isassigned(Arr(0.3), 2))
30+
@test (@inferred similar(Arr(0.3))) isa ZeroDimensionalArrayMutable{Float64}
31+
@test (@inferred similar(Arr(0.3), Float32)) isa ZeroDimensionalArrayMutable{Float32}
32+
@test fill(0.3) == Arr(0.3)
33+
@test Arr(0.3) == Arr(0.3)
34+
@test all(@inferred Arr(0.3) .== Arr(0.3))
35+
@test (@inferred Arr(0.3) .+ [10, 20]) isa AbstractVector
3536
end
3637
end
3738

@@ -40,13 +41,12 @@ using Aqua: Aqua
4041
@test @isdefined ZeroDimensionalArrayImmutable
4142
@test !ismutabletype(ZeroDimensionalArrayImmutable)
4243
@test isbitstype(ZeroDimensionalArrayImmutable{Float64})
43-
@test ZeroDimensionalArrayImmutable(fill(7)) === @inferred wrap_in_0dim(7)
4444
end
4545
@testset "`ZeroDimensionalArrayMutable`" begin
4646
@test @isdefined ZeroDimensionalArrayMutable
4747
@test ismutabletype(ZeroDimensionalArrayMutable)
4848
@test (@inferred ZeroDimensionalArrayMutable{Float32}()) isa ZeroDimensionalArrayMutable{Float32}
49-
@test let a = ZeroDimensionalArrayMutable(fill(0.3))
49+
@test let a = ZeroDimensionalArrayMutable(0.3)
5050
a[] = 0.7
5151
only(a) === 0.7
5252
end

0 commit comments

Comments
 (0)