Return with move semantics
Is that possible to use the move semantic when returning a value from a function?
The code below would give error:
error: 'Value[dtype]' is not copyable because it has no 'copyinit'
return v
^
mojo: error: failed to parse the provided Mojo source module
2 Replies
this should work
I'm not 100% sure if your intention is to consume v1 when calling foo, which is what should happen in my example or if the goal is to return a new value. You need moveinit on Value to move it, which the @value decorator provides. You also need to use the move operator
^
to specify that you'd like to move the value during the return rather than copy. If you'd like to move the new value then simply adding the move operator to your return statement in foo is all you need to do.thanks!
this works:
For now I kind of want to avoid using
@value
as I am still exploring the data model and not sure about their lifetimes.