Grandimam
Grandimam
MModular
Created by Grandimam on 4/12/2024 in #questions
Transfer operator inside constructor and moveinit
Understood! Apparently inside the method there is no way to guarantee that the value that method receives is a transfered value or not. It could actually be a copy of the input value. So, if we want to transfer again to the caller or some other method we need to explicitly add the suffix when returning the value. thanks @lukas!
11 replies
MModular
Created by Grandimam on 4/12/2024 in #questions
Transfer operator inside constructor and moveinit
yes, that part I understood. What I am unable to figure out is this part?
fn __moveinit__(inout self, owned existing: Self):
self.name = existing.name^
self.age = existing.age
fn __moveinit__(inout self, owned existing: Self):
self.name = existing.name^
self.age = existing.age
If I call this method (using the transfer operator)
example_movable(bar^)
example_movable(bar^)
it will trigger the moveinit method of MyPet struct. Now, since I am using the transfer operator while calling the example_movable() method, why do i need to use the ^ operator again while assiging it to self.name? Won't the below code work the same way?
fn __moveinit__(inout self, owned existing: Self):
self.name = existing.name
self.age = existing.age
fn __moveinit__(inout self, owned existing: Self):
self.name = existing.name
self.age = existing.age
11 replies
MModular
Created by Grandimam on 4/12/2024 in #questions
Transfer operator inside constructor and moveinit
I want to know why we are doing this - self.name = name^ inside a constructor or moveinit method?
11 replies
MModular
Created by seboll13 on 4/11/2024 in #questions
Easier way to sum a list of integers
I do not see aggregate functions in the doc. However, you can do something like this:
fn main():
var c: Int = 0
fn add(a: Int, /) capturing:
c += a
map[add](5)
fn main():
var c: Int = 0
fn add(a: Int, /) capturing:
c += a
map[add](5)
13 replies