ct
ct
MModular
Created by ct on 9/28/2023 in #questions
How can I create a Python list to add and remove structs?
I am trying mojo and would like to define a few structs and use a Python like dynamic list. I would like to be able to add and remove structs, iterate over items etc.
9 replies
MModular
Created by ct on 9/13/2023 in #questions
newbie needs help with error messages
the code:
@register_passable("trivial")
struct Item:
var name: StringLiteral
var qty: Int

fn __init__(inout self) -> Self:
self.name = "Unnamed"
self.qty = 0


fn main():
var item = Item()
@register_passable("trivial")
struct Item:
var name: StringLiteral
var qty: Int

fn __init__(inout self) -> Self:
self.name = "Unnamed"
self.qty = 0


fn main():
var item = Item()
gives error
invalid call to '__init__': callee expects 1 argument, but 0 were specified
invalid call to '__init__': callee expects 1 argument, but 0 were specified
what should I do?
20 replies
MModular
Created by ct on 9/13/2023 in #questions
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?
4 replies
MModular
Created by ct on 9/12/2023 in #questions
How to add an instance of a struct to a DynamicVector
A total newbie question. Example: struct Item: var name: StringLiteral fn init(inout self): self.name = "Unnamed" fn main(): var dv = DynamicVectorItem var it = Item() dv.push_back(it) Generates error: invalid call to 'push_back': method argument #0 cannot bind generic !mlirtype to memory-only type 'Item' I don't understand the error message. I cannot find in https://docs.modular.com/mojo/programming-manual.html the syntax for this. Is there an equivalent to c++ vector::emplace(...)?
1 replies