Skip to content

Commit 7559bd4

Browse files
add an undef initializer for LArray
1 parent a07f6c0 commit 7559bd4

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/larray.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ struct LArray{T, N, D <: AbstractArray{T, N}, Syms} <: DenseArray{T, N}
44
LArray{T, N, D, Syms}(__x) where {T, N, D, Syms} = new{T, N, D, Syms}(__x)
55
end
66

7+
function LArray{T, N, D, Syms}(::UndefInitializer, n::Int64) where {T, N, D, Syms}
8+
@assert length(Syms) == n
9+
LArray{T, N, D, Syms}(similar(D, n))
10+
end
11+
712
#####################################
813
# NamedTuple compatibility
914
#####################################
@@ -199,8 +204,8 @@ A.a == 1
199204
```
200205
201206
Users can also generate a labelled array with undefined values by instead giving
202-
the dimensions. This approach is useful if the user intends to pre-allocate an
203-
array for some later input.
207+
the dimensions. This approach is useful if the user intends to pre-allocate an
208+
array for some later input.
204209
205210
```julia
206211
A = @LArray Float64 (2,2) (:a,:b,:c,:d)
@@ -226,7 +231,7 @@ julia> z.a
226231
4
227232
```
228233
229-
The labels of LArray and SLArray can be accessed
234+
The labels of LArray and SLArray can be accessed
230235
by function `symbols`, which returns a tuple of symbols.
231236
"""
232237
macro LArray(vals, syms)
@@ -260,7 +265,7 @@ A = @LVector Float64 (:a,:b,:c,:d)
260265
A .= rand(4)
261266
```
262267
263-
On the other hand, users can also initialize the vector and set its values at the
268+
On the other hand, users can also initialize the vector and set its values at the
264269
same time:
265270
266271
```julia

test/larrays.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ using LabelledArrays, Test, InteractiveUtils
1414
mat = rand(4, 3)
1515
@test mat * vals mat * [1.0, 2.0, 3.0]
1616
syms = (:a, :b, :c)
17+
@test typeof(typeof(x)(undef,3)) == typeof(x)
1718

1819
for (i, s) in enumerate(syms)
1920
@show i, s

0 commit comments

Comments
 (0)