Best way to make a struct of CollectionElements to conform to CollectionElement.

I am still confused about best practices for __moveinit__ and __copyinit__. Let's say i have a struct with two variables, which are both structs which conform to the CollectionElement trait, how to define __moveinit__ and __copyinit__ for this struct. Will this do?
struct A:
var b:B # B conforms to CollectionElement
var c:C # C conforms to CollectionElement
fn __init__(...

fn __copyinit__(inout self, existing: Self):
# ????
self.b = existing.b
self.c = existing.c

fn __moveinit__(inout self, owned existing: Self):
# ???
self.b = existing.b
self.c = existing.c
struct A:
var b:B # B conforms to CollectionElement
var c:C # C conforms to CollectionElement
fn __init__(...

fn __copyinit__(inout self, existing: Self):
# ????
self.b = existing.b
self.c = existing.c

fn __moveinit__(inout self, owned existing: Self):
# ???
self.b = existing.b
self.c = existing.c
Any advice highly appreciated
2 Replies
Ryulord
Ryulord4mo ago
Unless you're doing anything special this should work just fine. moveinit and copyinits like this are so common that it's why we have @value. Almost every struct I write uses the value decorator.
drujensen
drujensen4mo ago
sometimes I see in the moveinit a caret ^ symbol at the end of the assignment.
fn __moveinit__(inout self, owned other: Graph):
self.inputs = other.inputs^
self.params = other.params^
self.nodes = other.nodes^
self.outputs = other.outputs^
self.loss_out = other.loss_out
self.symbol_count = other.symbol_count
fn __moveinit__(inout self, owned other: Graph):
self.inputs = other.inputs^
self.params = other.params^
self.nodes = other.nodes^
self.outputs = other.outputs^
self.loss_out = other.loss_out
self.symbol_count = other.symbol_count
Does that mean copy the address instead of the value? If so, why not do this for all the fields? Is it something to do with primitive types? nm, found the answer about ^ here: https://discord.com/channels/1087530497313357884/1247713442190721114/1247715698092802119
Want results from more Discord servers?
Add your server