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

Did you find this page helpful?