Skip to content

Commit 9ae5a45

Browse files
authored
rename ZeroDimensionalArrayMutable* to Box* (#13)
Updates #7
1 parent 5a6e165 commit 9ae5a45

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ Exports three zero-dimensional subtypes of `AbstractArray`:
1313
* declared with `struct`, not with `mutable struct`
1414
* does not support `setfield!`, or mutating the element otherwise
1515
* `isbits` when the element is `isbits`
16-
* `ZeroDimensionalArrayMutable`
16+
* `Box`
1717
* declared with `mutable struct`
1818
* supports `setfield!`
19-
* `ZeroDimensionalArrayMutableConstField`
19+
* `BoxConstField`
2020
* declared with `mutable struct`
2121
* does not support `setfield!`, or mutating the element otherwise
2222
* included for completeness, but not likely to be useful often
@@ -28,17 +28,17 @@ The motivation for creating this package is:
2828
* https://discourse.julialang.org/t/ref-is-not-a-concrete-type-poorly-documented/120375
2929
* https://discourse.julialang.org/t/ref-t-vs-base-refvalue-t/127886/
3030
* To provide "mutable wrapper" functionality, something `Ref` is often used for:
31-
* `ZeroDimensionalArrayMutable` can be a good replacement. Examples:
31+
* `Box` can be a good replacement. Examples:
3232
* make a `const` binding that's mutable:
3333
```julia
34-
const some_const_binding = ZeroDimensionalArrayMutable(0.2)
34+
const some_const_binding = Box(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
3838
struct SomeImmutableType
3939
immutable_bool::Bool
4040
immutable_float::Float64
41-
mutable_int::ZeroDimensionalArrayMutable{Int}
41+
mutable_int::Box{Int}
4242
end
4343
```
4444
* previous discussion:

src/ZeroDimensionalArrays.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ module ZeroDimensionalArrays
22

33
export
44
ZeroDimensionalArrayImmutable,
5-
ZeroDimensionalArrayMutable,
6-
ZeroDimensionalArrayMutableConstField
5+
Box,
6+
BoxConstField
77

88
struct ZeroDimensionalArrayImmutable{T} <: AbstractArray{T, 0}
99
v::T
@@ -12,7 +12,7 @@ struct ZeroDimensionalArrayImmutable{T} <: AbstractArray{T, 0}
1212
end
1313
end
1414

15-
mutable struct ZeroDimensionalArrayMutable{T} <: AbstractArray{T, 0}
15+
mutable struct Box{T} <: AbstractArray{T, 0}
1616
v::T
1717
global function new_zero_dimensional_array_mutable(::Type{T}, v) where {T}
1818
new{T}(v)
@@ -22,7 +22,7 @@ mutable struct ZeroDimensionalArrayMutable{T} <: AbstractArray{T, 0}
2222
end
2323
end
2424

25-
mutable struct ZeroDimensionalArrayMutableConstField{T} <: AbstractArray{T, 0}
25+
mutable struct BoxConstField{T} <: AbstractArray{T, 0}
2626
const v::T
2727
global function new_zero_dimensional_array_mutable_const_field(::Type{T}, v) where {T}
2828
new{T}(v)
@@ -31,16 +31,16 @@ end
3131

3232
const ZeroDimensionalArray = Union{
3333
ZeroDimensionalArrayImmutable,
34-
ZeroDimensionalArrayMutable,
35-
ZeroDimensionalArrayMutableConstField,
34+
Box,
35+
BoxConstField,
3636
}
3737

3838
function type_to_constructor_function(::Type{T}) where {T <: ZeroDimensionalArray}
3939
if T <: ZeroDimensionalArrayImmutable
4040
new_zero_dimensional_array_immutable
41-
elseif T <: ZeroDimensionalArrayMutable
41+
elseif T <: Box
4242
new_zero_dimensional_array_mutable
43-
elseif T <: ZeroDimensionalArrayMutableConstField
43+
elseif T <: BoxConstField
4444
new_zero_dimensional_array_mutable_const_field
4545
else
4646
throw(ArgumentError("no such constructor function"))
@@ -63,7 +63,7 @@ function Base.getindex(a::ZeroDimensionalArray)
6363
a.v
6464
end
6565

66-
function Base.setindex!(a::ZeroDimensionalArrayMutable, x)
66+
function Base.setindex!(a::Box, x)
6767
a.v = x
6868
end
6969

@@ -101,8 +101,8 @@ end
101101

102102
for Arr (
103103
ZeroDimensionalArrayImmutable,
104-
ZeroDimensionalArrayMutable,
105-
ZeroDimensionalArrayMutableConstField,
104+
Box,
105+
BoxConstField,
106106
)
107107
@eval begin
108108
function Base.convert(::Type{$Arr}, a::AbstractArray{<:Any, 0})
@@ -120,7 +120,7 @@ for Arr ∈ (
120120
end
121121
end
122122

123-
function ZeroDimensionalArrayMutable{T}() where {T}
123+
function Box{T}() where {T}
124124
new_zero_dimensional_array_mutable_undef(T)
125125
end
126126

@@ -136,7 +136,7 @@ end
136136
# ```
137137

138138
# https://github.com/JuliaLang/julia/issues/51753
139-
if isdefined(Base, :dataids) && hasmethod(Base.dataids, Tuple{ZeroDimensionalArrayMutable{Float32}})
139+
if isdefined(Base, :dataids) && hasmethod(Base.dataids, Tuple{Box{Float32}})
140140
function Base.dataids(a::ZeroDimensionalArray)
141141
Base.dataids(only(a))
142142
end

test/runtests.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ using Aqua: Aqua
1010
@testset "all array types joined" begin
1111
for Arr (
1212
ZeroDimensionalArrayImmutable,
13-
ZeroDimensionalArrayMutable,
14-
ZeroDimensionalArrayMutableConstField,
13+
Box,
14+
BoxConstField,
1515
)
1616
@test (@inferred Arr(0.3)) == (@inferred convert(Arr, fill(0.3)))
1717
@test isstructtype(Arr)
@@ -27,8 +27,8 @@ using Aqua: Aqua
2727
@test @inferred isassigned(Arr(0.3))
2828
@test @inferred isassigned(Arr(0.3), 1)
2929
@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}
30+
@test (@inferred similar(Arr(0.3))) isa Box{Float64}
31+
@test (@inferred similar(Arr(0.3), Float32)) isa Box{Float32}
3232
@test fill(0.3) == Arr(0.3)
3333
@test Arr(0.3) == Arr(0.3)
3434
@test all(@inferred Arr(0.3) .== Arr(0.3))
@@ -42,18 +42,18 @@ using Aqua: Aqua
4242
@test !ismutabletype(ZeroDimensionalArrayImmutable)
4343
@test isbitstype(ZeroDimensionalArrayImmutable{Float64})
4444
end
45-
@testset "`ZeroDimensionalArrayMutable`" begin
46-
@test @isdefined ZeroDimensionalArrayMutable
47-
@test ismutabletype(ZeroDimensionalArrayMutable)
48-
@test (@inferred ZeroDimensionalArrayMutable{Float32}()) isa ZeroDimensionalArrayMutable{Float32}
49-
@test let a = ZeroDimensionalArrayMutable(0.3)
45+
@testset "`Box`" begin
46+
@test @isdefined Box
47+
@test ismutabletype(Box)
48+
@test (@inferred Box{Float32}()) isa Box{Float32}
49+
@test let a = Box(0.3)
5050
a[] = 0.7
5151
only(a) === 0.7
5252
end
5353
end
54-
@testset "`ZeroDimensionalArrayMutableConstField`" begin
55-
@test @isdefined ZeroDimensionalArrayMutableConstField
56-
@test ismutabletype(ZeroDimensionalArrayMutableConstField)
54+
@testset "`BoxConstField`" begin
55+
@test @isdefined BoxConstField
56+
@test ismutabletype(BoxConstField)
5757
end
5858
end
5959
end

0 commit comments

Comments
 (0)