8

Say that I have an array of some composite type in Julia. I understand that I can't simply assign values into the array since its elements are undefined. For example the code

type struct
  u::Int64
  v::Int64
end

X = Array(struct, 100)
X[10].u = 3

will generate this error:

ERROR: access to undefined reference
 in getindex at array.jl:277
 in include at boot.jl:238
 in include_from_node1 at loading.jl:114

What is the standard way to deal with this? For now I am just doing something like:

samples = Array(Sample1d, num_samples)
fill!(samples, Sample1d(0, 0, 0))
samples[i] = ...

Is there a more concise or Julian way to do this?

2 Answers 2

7

You can use fill to create and fill an array at the same time:

type struct
  u::Int
  v::Int
end

struct() = struct(0, 0)
X = fill(struct(), 100)
X[10].u = 3
2

You can assign values to uninitialized locations in arrays. You just can't extract values from uninitialized locations.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.