I’m not sure how to boil this down to one simple question. I have a Struct named LumpOfData with several properties, let’s just make it one named x, and in a class Foo I create an array of these. There is a known fixed number of these, so I allocate an array of that size, ready to be indexed and have values assigned indexed randomly. My LumpOfData has a constructor taking parameters, just one in this case.
struct LumpOfData property x def initialize(@x : Float32) end end class Foo def initialize(n) @lumps = Array(LumpOfData).new(n) end def add_new_lump(i : Int32, newlump : LumpOfData) if @lumps[i] already has a value throw an exception or whatever else lumps[i] = newlump end lumps end end
Elsewhere in this software, I’d be writing code like:
foo.add_new_lump( 45, LumpOfData.new(0.23) )
It’s unclear from documentation (https://crystal-lang.org/reference/ and https://crystal-lang.org/api/0.31.1/index.html mostly) what happens when I allocate the array. Are all the structs initialized with zeros, NaNs, or some default values? (I’m not actually using Float in my code, that’s just for illustration here) How would I tell if some element, say , has had a meaningful value assigned or not?
Note I’m coming from C, C++, D and years ago, Pascal (ick!), BASIC (ickier!) and assembly and hands-on electronics, and tend to think differently than those who went through computer science classes. I’m probably thinking about this in a quirky way, trying to write C in Crystal.