Store Reference in struct
Is there an example on how to create a struct with a Reference field?
17 Replies
I saw a couple examples from Chris here if it's helpful https://github.com/modularml/mojo/discussions/1568#discussioncomment-8251023
GitHub
Inferred Mutability: Reachability-based reference lifetime/mutabili...
Summary of idea: The borrow checker shall track the reachability from one reference to another reference within the function (whenever feasible). Inter-procedurally, the type signature may associat...
Is someone able to provide a real example of a struct with a reference as a field? This would be greatly appreciated. 🙂
(The examples by Chris does not specify what thisLifetime actually is in an instantiation)
In this example, if the struct rs is returned, I wonder how it works, since the string it's pointing to is on the stack and will be destroyed when the function returns
Sure this works. But what if we want to use other structs than Strings? E.g. a Tensor or something? Then what is L?
I think it's the same
I t is not:
@value
struct RefTensor[L: MutLifetime]:
var tensor_ref: Reference[Tensor, __mlir_attr.
1: i1
, L]
fn main():
var a = TensorDType.float32
var r = Reference(a)
var rs = RefTensor(r) # Error: cannot construct 'RefTensor[?]' from 'Reference[Tensor[f32], 1, *"a`0"]' value in 'var' initializeri’ve had trouble getting References working well, idk that they are ready for production code yet
should be
Tensor was missing its type parameter.
crazy times
Congrats @TilliFe, you just advanced to level 3!
thank you
I think this is what Reference is designed to fix. If I understand your question correctly, this example demonstrates how that works .
I think the problem you are describing is what References and Lifetimes are designed to manage. The lifetime of
a
gets shared with its references and prevents the immediate destruction of a
after its last use. I altered the last line so a
is not used and you can see the result still prints correctly because the string has not been destroyed.I had in my mind something like this, wondering how it would work:
But that can't work because when calling
some_factory_func
, it's not possible to specify a lifetime yet.
(i get /projects/open_source/mojo-stdlib-extensions/trying_stuff.mojo:5:27: error: 'RefString' expects 1 input parameter, but 0 were specified, fn some_factory_func() -> RefString:
)
Furthermore, the variable a
is on the stack, thus it will be destroyed when some_factory_func
returns.Congrats @gabrieldemarmiesse, you just advanced to level 8!
I guess that maybe it would work if
a
was on the heap and not on the stack? But I'm unsure on how to do this with MojoIs there some way to explicitly pass in
L
? I would like to have a RefString in the field of another struct