Is it possible to store a list of References in Struct?
Calling all reference experts! I've seen some conversations lightly touching on the topic but I haven't seen anything concrete. I know I can do something like this:
But I can't actually add any references to the list, the lifetimes won't match. Is this something that's not possible in Mojo's current state?
7 Replies
you could try making two lists for both possible mutabilities inside a struct, that should hopefully solve the issue
Sorry, I’m not following 😅 do you have any examples?
It is possible if the items going into the list share a lifetime. If they are stored together in some other structure you can use that structures lifetime for the list. Or maybe you could use a MutableStaticLifetime for the list.
Something like this
Ahh that makes sense. Seems like a list of refs isn’t the approach I want.
I was hoping to define and configure Command structs, and add references to them to the CLI struct. But maybe it would be better to transfer ownership of the commands to the CLI struct and add the configuration functions to the CLI struct instead
You probably want to use
Arc
:
@Ethan Thanks for offering some advice! I'm using Arc for the current implementation, it does the job for the most part aside from needing to save arc function args as a variable before it can be dereferenced.
I’m glad it worked for you.