M
Modular15mo 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
superfes15mo ago
New here, but I don't get that error when running it.
ct
ctOP15mo 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
superfes15mo ago
No description
superfes
superfes15mo ago
Weird
ct
ctOP15mo ago
mojo --version mojo 0.2.1 (64d14e85)
superfes
superfes15mo ago
I did in fact not have 0.2.1 installed Sorry
ct
ctOP15mo ago
do you have a newer version?
superfes
superfes15mo ago
Older Just ran the install function again
ct
ctOP15mo ago
does it download the 350MB or so or is it smart about it?
superfes
superfes15mo ago
Heh, I dunno how much it downloaded
ct
ctOP15mo 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
superfes15mo 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
ModularBot15mo ago
Congrats @superfes, you just advanced to level 1!
ct
ctOP15mo ago
I think the parens are necessary and should call the __init__ with "self" being provided
ModularBot
ModularBot15mo ago
Congrats @ct, you just advanced to level 2!
Want results from more Discord servers?
Add your server