M
Modular16mo ago
ct

What kind of list should I use to add struct instances

Example struct:
struct Item:
var name: StringLiteral
fn __init__(inout self):
self.name = "Unnamed"
struct Item:
var name: StringLiteral
fn __init__(inout self):
self.name = "Unnamed"
DynamicVector cannot contain this struct instance because it is no register storable. What other type of list should I use? What would be a sample code for that?
3 Replies
Stole
Stole16mo ago
There isn't currently any list type that supports non-register-passable structs, Pointers to non-register-passable structs cannot be .load()-ed. However, if you wanna try slapping @register_passable("trivial") on anything, go ahead! I can't test anything right now, but I think this Item struct could be since I just learned that StringLiteral is register passable or equivalent. Beware, though. There are a multitude of lifetime issues associated with dealing with this. Make sure you know where you're making a shallow copy of a struct and where you want to actually copy the pointed-to-memory with memcpy or similar.
ct
ctOP16mo ago
Thanks for the reply.
yogidrink
yogidrink15mo ago
So that means there currently is no real way to create a tree structure with nodes which have children referenced? This currently doesnt work
from utils.vector import DynamicVector

@register_passable("trivial")
struct Node:
var children: DynamicVector[Self]

fn __init__(inout self) -> Self:
let c = DynamicVector[Self]()
return Node {children: c}

Node()
from utils.vector import DynamicVector

@register_passable("trivial")
struct Node:
var children: DynamicVector[Self]

fn __init__(inout self) -> Self:
let c = DynamicVector[Self]()
return Node {children: c}

Node()
Want results from more Discord servers?
Add your server