M
Modular7mo ago
Aamir

Seeking Guidance and Best Practices for Lists and Tuples in Mojo Language

Hello Wonderful Mojo Community! I'm excited to embark on my journey with the Mojo language and am particularly interested in developing datetime and strings libraries. As a newcomer, I'm eager to learn and contribute to this vibrant community. However, I've encountered a bit of a hurdle while working with lists and tuples. Unlike Python, where I can easily access items in a list or tuple, in Mojo, I receive an error stating: it does not implement the '__getitem__'/'__setitem__' or '__refitem__' methods. I'm keen to understand the best practices for handling lists and tuples in Mojo. Could anyone provide guidance or share their experiences on how to navigate this? I'm looking forward to your insights and tips to enhance my understanding and skills in Mojo. Thank you all in advance for your support and assistance!
25 Replies
ModularBot
ModularBot7mo ago
Congrats @Aamir, you just advanced to level 1!
brainiacoutcast
brainiacoutcast7mo ago
you're probably used to using square brackets on python's lists, for mojo's ListLiteral you'll need to use the .get method https://docs.modular.com/mojo/stdlib/builtin/builtin_list.html#get or alternatively, you might be able to just use a dynamicvector if your items in the list are all the same type
Aamir
Aamir7mo ago
Yeah, I had tried that. However, I dont know the right way to invoke get function. It didn't work for me!
No description
Aamir
Aamir7mo ago
Is there any book or guided language tour that I can try?
Aamir
Aamir7mo ago
Also, when I tried to import DynamicVector, I get this error!
No description
Aamir
Aamir7mo ago
I am using mojo 0.7.0 (af002202) on windows Ubuntu WSL.
benny
benny7mo ago
dynamic vector should be in collections.vector, not Vector. Along with this, the .get function takes a compile time constant parameter, not a variable, so you would need to say something like tuple.get0, Int to get the first item of a tuple of type int
Aamir
Aamir7mo ago
I need to pass, get value at the given 'i index.
No description
Aamir
Aamir7mo ago
About Vector import. I followed this!
No description
Aamir
Aamir7mo ago
After importing the DynamicVector from collections. That error went away, but it started crashing again!
No description
Aamir
Aamir7mo ago
Compiler is crashing now! Is this language stable? I am not able to move an inch. There should be proper guided language tour with documentations. The way Go did.
brainiacoutcast
brainiacoutcast7mo ago
it's fairly common to have the parser crashing, remember that this language is pre 1.0, you are helping with early testing
Aamir
Aamir7mo ago
I see! I understand, no worries
brainiacoutcast
brainiacoutcast7mo ago
while it may take some fiddling to get the dynamicvector working, you're on the right path to getting the program working if you need to index into a collection with a value from a variable
Aamir
Aamir7mo ago
But, after adding dynamic vector, I can't move ahead, due to parser crash. How do you guyw deal with such issues.
Want results from more Discord servers?
Add your server