In https://github.com/dmbates/Wordlegames.jl I use a struct to represent the state of a Wordle (or similar) game. I am thinking of recording the history of guesses and scores from these guess as fields which will be Vector{<:Integer}. (In Wordle “Hard Mode” the guess must always be from the current target pool and it can be represented as an index into the initial target pool. Similarly a score can be regarded as a number in base-3.)
Because Wordle allows only 6 guesses, the vectors could be fixed-length. But I would prefer to avoid a restriction like that in the design, because I want to analyze related games that may need more than 6 guesses. Also, it would be convenient to empty! this container at the beginning of the game and push! results onto it so it is easy to determine things like what the last guess was.
If I declare the field to be, say, Vector{Int} and initialize it to, say, sizehint!(Int[], 10), can I safely push! to it even though it is a field in a struct. The situation I am trying to avoid is push! causing a re-allocate and copy when the available storage for the vector is exhausted. It is fine to change the contents of a field in a struct but not the location.
An immutable object might contain mutable objects, such as arrays, as fields. Those contained objects will remain mutable; only the fields of the immutable object itself cannot be changed to point to different objects.
Thanks for the reply. I may not have phrased it well but I realize that I can change the contents of a field but I can’t rebind it. What I was asking specifically is what happens if push! causes the rebinding behind the scenes?
As I understand it, when using a Vector as a stack or a deque the memory for the vector is over-allocated initially so that elements can be pushed on to the end. The sizehint! function allows the programmer to give hints on the initial size. But if you try to push! beyond the allocated memory the operation has to allocate a larger chunk and copy the current contents into the new location. At least, that is how I understand it works.
So there could be a situation where several push! calls are benign but then one comes along that causes a reallocation and hence a rebinding. What happens in that situation?
I think the confusion is that the Julia-level Vector is not rebound when push! does something (IIUC). In other words, push! might re-allocate a new contiguous block of memory, but that only changes its internal pointer to that memory, not the Julia-level binding to your Vector.
I was planning to try it but I thought I would ask here first in case someone knew the answer immediately.
It turns out that my idea of how a Vector is implemented was wrong. I hadn’t realized that there was the extra level of indirection that @palday mentioned.