Skip to content

Commit d54a41c

Browse files
authored
add new exported function for constructing from element, README (#3)
1 parent 89f7042 commit d54a41c

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,57 @@
11
# ZeroDimensionalArrays
22

33
[![Build Status](https://github.com/JuliaArrays/ZeroDimensionalArrays.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/JuliaArrays/ZeroDimensionalArrays.jl/actions/workflows/CI.yml?query=branch%3Amain)
4+
[![Package version](https://juliahub.com/docs/General/ZeroDimensionalArrays/stable/version.svg)](https://juliahub.com/ui/Packages/General/ZeroDimensionalArrays)
5+
[![Package dependencies](https://juliahub.com/docs/General/ZeroDimensionalArrays/stable/deps.svg)](https://juliahub.com/ui/Packages/General/ZeroDimensionalArrays?t=2)
46
[![PkgEval](https://JuliaCI.github.io/NanosoldierReports/pkgeval_badges/Z/ZeroDimensionalArrays.svg)](https://JuliaCI.github.io/NanosoldierReports/pkgeval_badges/Z/ZeroDimensionalArrays.html)
57
[![Aqua](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl)
8+
9+
A small software package for the Julia programming language providing zero-dimensional array types. `Ref`-killer.
10+
11+
Exports three zero-dimensional subtypes of `AbstractArray`:
12+
* `ZeroDimensionalArrayImmutable`
13+
* declared with `struct`, not with `mutable struct`
14+
* does not support `setfield!`, or mutating the element otherwise
15+
* `isbits` when the element is `isbits`
16+
* `ZeroDimensionalArrayMutable`
17+
* declared with `mutable struct`
18+
* supports `setfield!`
19+
* `ZeroDimensionalArrayMutableConstField`
20+
* declared with `mutable struct`
21+
* does not support `setfield!`, or mutating the element otherwise
22+
* included for completeness, but not likely to be useful often
23+
24+
The motivation for creating this package is:
25+
* To prevent the frequent confusion regarding `Ref` vs `Base.RefValue` by offering a replacement that makes `Ref` unnecessary in many use cases. Previous discussion:
26+
* https://github.com/JuliaLang/julia/issues/38133
27+
* https://github.com/JuliaLang/julia/issues/55321
28+
* https://discourse.julialang.org/t/ref-is-not-a-concrete-type-poorly-documented/120375
29+
* https://discourse.julialang.org/t/ref-t-vs-base-refvalue-t/127886/
30+
* To provide "mutable wrapper" functionality, something `Ref` is often used for:
31+
* `ZeroDimensionalArrayMutable` can be a good replacement. Examples:
32+
* make a `const` binding that's mutable:
33+
```julia
34+
const some_const_binding = ZeroDimensionalArrayMutable(fill(0.2))
35+
```
36+
* make a field within an immutable `struct` mutable (warning: it's usually more efficient to change the entire `struct` into a `mutable struct`)
37+
```julia
38+
struct SomeImmutableType
39+
immutable_bool::Bool
40+
immutable_float::Float64
41+
mutable_int::ZeroDimensionalArrayMutable{Int}
42+
end
43+
```
44+
* previous discussion:
45+
* https://github.com/JuliaLang/julia/issues/40369
46+
* https://discourse.julialang.org/t/dynamic-immutable-type/127168
47+
* 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:
49+
```julia-repl
50+
julia> using ZeroDimensionalArrays
51+
52+
julia> isa.(wrap_in_0dim([1,2,3]), [Array, Dict, Int])
53+
3-element BitVector:
54+
1
55+
0
56+
0
57+
```

src/ZeroDimensionalArrays.jl

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

33
export
4+
wrap_in_0dim,
45
ZeroDimensionalArrayImmutable,
56
ZeroDimensionalArrayMutable,
67
ZeroDimensionalArrayMutableConstField
@@ -29,6 +30,10 @@ mutable struct ZeroDimensionalArrayMutableConstField{T} <: AbstractArray{T, 0}
2930
end
3031
end
3132

33+
function wrap_in_0dim(v)
34+
new_zero_dimensional_array_immutable(typeof(v), v)
35+
end
36+
3237
const ZeroDimensionalArray = Union{
3338
ZeroDimensionalArrayImmutable,
3439
ZeroDimensionalArrayMutable,

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ using Aqua: Aqua
3939
@test @isdefined ZeroDimensionalArrayImmutable
4040
@test !ismutabletype(ZeroDimensionalArrayImmutable)
4141
@test isbitstype(ZeroDimensionalArrayImmutable{Float64})
42+
@test ZeroDimensionalArrayImmutable(fill(7)) === @inferred wrap_in_0dim(7)
4243
end
4344
@testset "`ZeroDimensionalArrayMutable`" begin
4445
@test @isdefined ZeroDimensionalArrayMutable

0 commit comments

Comments
 (0)