M
Modular•6mo ago
Frank Saez

Question about iterating a list of strings

Hi, I've got a background in Rust (10 years) and in Python (1 year) and I try to learn Mojo. I've try this :
fn main():
try:
var stuff = String("dddd\nffff\njjjj\nkkkk").split("\n")
for x in stuff:
print(x)
except:
print("failed")
fn main():
try:
var stuff = String("dddd\nffff\njjjj\nkkkk").split("\n")
for x in stuff:
print(x)
except:
print("failed")
and got this obscure error message :
/source/prog.mojo:7:12: error: no matching function in call to 'print'
print(x)
~~~~~^~~
/source/prog.mojo:1:1: note: candidate not viable: expected at most 0 positional arguments, got 1
fn main():
^
/source/prog.mojo:1:1: note: candidate not viable: callee expects 2 parameters, but 0 were specified
fn main():
^
/source/prog.mojo:7:13: note: failed to infer parameter 'T', argument type 'Reference[String, 1, stuff, 0]' does not conform to trait 'Stringable'
print(x)
^
mojo: error: failed to parse the provided Mojo source module
/source/prog.mojo:7:12: error: no matching function in call to 'print'
print(x)
~~~~~^~~
/source/prog.mojo:1:1: note: candidate not viable: expected at most 0 positional arguments, got 1
fn main():
^
/source/prog.mojo:1:1: note: candidate not viable: callee expects 2 parameters, but 0 were specified
fn main():
^
/source/prog.mojo:7:13: note: failed to infer parameter 'T', argument type 'Reference[String, 1, stuff, 0]' does not conform to trait 'Stringable'
print(x)
^
mojo: error: failed to parse the provided Mojo source module
but if I use indexes, it works :
for x in range(0, len(stuff)):
print(stuff[x])
for x in range(0, len(stuff)):
print(stuff[x])
I seem to have a hard time with the most basic things 🙂 What have I misunderstood ? Thanks
8 Replies
Serg Gini
Serg Gini•6mo ago
You need to use print(x[]) for 'dereference' or something like that. I've seen it in the other code, but still don't understand this model yet by myself)
Frank Saez
Frank SaezOP•6mo ago
thanks !
Melody Daniel
Melody Daniel•6mo ago
Auto-deref has been implemented and will slowly get added to the stdlib so you wouldn't need to deference pointers yourself in the future
Frank Saez
Frank SaezOP•6mo ago
@FSaez good to know, thanks.
Moosems / Three chickens
Yes. There’s a plan for auto dereferencing. @FSaez You should open an issue for a wrong or unhelpful error message
ilrico
ilrico•5mo ago
I was wondering, any particular reason to use [] operator to deference, instead of widly-accepted *, present in mainstream languages? I understand the "access to value" meaning of [], close to array[index], but asterisk is so familiar to programmers...
Melody Daniel
Melody Daniel•5mo ago
"*" is already heavily overloaded in Python Also, the square bracket acts as a getter. I believe ref[] is the same as ref[0] or something like that
ilrico
ilrico•5mo ago
yes, the getter meaning is what I called "access to value". But * overloading is indeed a very valid explanation. thx
Want results from more Discord servers?
Add your server