M
Modular13mo ago
ct

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?
16 Replies
superfes
superfes13mo ago
New here, but I don't get that error when running it.
ct
ct13mo ago
this is what I get
@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 = Item()
var ls = DynamicVector[Item]()
ls.push_back(item)
print(ls[0].name)
c@c:~/w/mojo-playground$ mojo hello.mojo
hello.mojo:15:26: error: invalid call to '__init__': callee expects 1 argument, but 0 were specified
var item: Item = Item()
~~~~^~
hello.mojo:9:5: note: function declared here
fn __init__(inout self) -> Self:
^
mojo: error: failed to parse the provided Mojo
@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 = Item()
var ls = DynamicVector[Item]()
ls.push_back(item)
print(ls[0].name)
c@c:~/w/mojo-playground$ mojo hello.mojo
hello.mojo:15:26: error: invalid call to '__init__': callee expects 1 argument, but 0 were specified
var item: Item = Item()
~~~~^~
hello.mojo:9:5: note: function declared here
fn __init__(inout self) -> Self:
^
mojo: error: failed to parse the provided Mojo
superfes
superfes13mo ago
No description
superfes
superfes13mo ago
Weird
ct
ct13mo ago
mojo --version mojo 0.2.1 (64d14e85)
superfes
superfes13mo ago
I did in fact not have 0.2.1 installed Sorry
ct
ct13mo ago
do you have a newer version?
superfes
superfes13mo ago
Older Just ran the install function again
ct
ct13mo ago
does it download the 350MB or so or is it smart about it?
superfes
superfes13mo ago
Heh, I dunno how much it downloaded
ct
ct13mo ago
This is what I got now:
@register_passable("trivial")
struct Item:
var name: StringLiteral
var qty: Int

fn __init__(inout self) -> Self:
return Self {name: "Unnamed", qty: 0}


fn main():
var item = Item()
var ls = DynamicVector[Item]()
ls.push_back(item)
print(ls[0].name)
print(ls[0].qty)
@register_passable("trivial")
struct Item:
var name: StringLiteral
var qty: Int

fn __init__(inout self) -> Self:
return Self {name: "Unnamed", qty: 0}


fn main():
var item = Item()
var ls = DynamicVector[Item]()
ls.push_back(item)
print(ls[0].name)
print(ls[0].qty)
error:
error: invalid call to '__init__': callee expects 1 argument, but 0 were specified
var item = Item()
error: invalid call to '__init__': callee expects 1 argument, but 0 were specified
var item = Item()
superfes
superfes13mo ago
Just FYI, when I get rid of the parens for var item = Item() it complains about something else error: return expected at end of function with results Which I assume is what you're after?
ModularBot
ModularBot13mo ago
Congrats @superfes, you just advanced to level 1!
ct
ct13mo ago
I think the parens are necessary and should call the __init__ with "self" being provided
ModularBot
ModularBot13mo ago
Congrats @ct, you just advanced to level 2!
Want results from more Discord servers?
Add your server