Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] implement __getitem__ for ListLiteral #2141

Open
1 task done
kiran-4444 opened this issue Apr 2, 2024 · 7 comments
Open
1 task done

[Feature Request] implement __getitem__ for ListLiteral #2141

kiran-4444 opened this issue Apr 2, 2024 · 7 comments
Labels
enhancement New feature or request mojo-repo Tag all issues with this label

Comments

@kiran-4444
Copy link

Review Mojo's priorities

What is your request?

I see this issue being referred in #100 and few other places, but __getitem__ isn't still implemented (or at least I wasn't able to access an item using it's index in a ListLiteral).

What is your motivation for this change?

I'm trying to work on #2018 and found that I need to iterate through a list of possible env variables that define the temp dirs names. But I'm surprised that I can't access the list items using their indices:

fn _candidate_tempdir_list() raises:
    var dirlist = []

    var env_vars = ["TMPDIR", "TEMP", "TMP"]
    for i in range(len(env_vars)):
        var env_var = env_vars.get(i)

Any other details?

I'd like to implement this if someone else isn't already working on this.

@kiran-4444 kiran-4444 added enhancement New feature or request mojo Issues that are related to mojo labels Apr 2, 2024
@melodyogonna
Copy link

melodyogonna commented Apr 2, 2024

I don't think that is a thing. Where would it be useful?

@kiran-4444
Copy link
Author

This, I'm using to implement the code from: https://github.com/python/cpython/blob/main/Lib/tempfile.py#L156-L181

Basically, I'm thinking of a bit solid implementation for tempfile and tempdir just like Python's implementation.

@ematejska ematejska added the mojo-stdlib Tag for issues related to standard library label Apr 2, 2024
@kiran-4444
Copy link
Author

Should I go ahead with the implementation?

@artemiogr97
Copy link
Contributor

@kiran-4444 for #2018 I guess you could simply iterate over a list for now, the following code prints all the elements of the list:

fn main():
    var my_vars = List("PATH", "TEMP")
    var index: Int
    for index in range(len(my_vars)):
        print(my_vars[index])

I agree that iterating over a ListLiteral is usefull but is not blocking for the other issue

@helehex
Copy link
Contributor

helehex commented Apr 6, 2024

ListLiteral is heterogeneous, meaning the elements it contains can have different types, and the type at each index is known at compile time. Also, since it's meant to be a literal type, it cant be changed in any way at runtime.

You should be able to do something like this though:
ListLiteral("Hi", "Kiran", "-4444").get[1,StringLiteral]() should return "kiran"

Maybe you could use a regular List for what you want to do?
List("Hi", "Kiran", "-4444")[1] should return "kiran"

It might be tricky to promote ListLiteral to a List currently due to their generics. This will be changing eventually though.
Also, for autopromotion you might need something like this

@linear linear bot removed the mojo Issues that are related to mojo label Apr 29, 2024
@linear linear bot changed the title [Feature Request] implement __getitem__ for ListLiteral [Feature Request] implement __getitem__ for ListLiteral Apr 29, 2024
@linear linear bot removed the mojo-stdlib Tag for issues related to standard library label Apr 29, 2024
@ematejska ematejska added the mojo-repo Tag all issues with this label label Apr 29, 2024
@guna-sd
Copy link

guna-sd commented May 31, 2024

In Python, we can initialize a list with a simple syntax like this:

a = [1, 2, 'a']

Mojo offers a similar initialization syntax for a list literal:

var a = [1, 2, 'a']

However, there are some key differences between Python lists and Mojo's list literals. Python lists support a wide range of operations, including indexing, slicing, getitem, setitem, and more. In contrast, Mojo's list literals currently lack these features, cannot be iterated over directly, and require a parametric index instead of an argument index.

While Mojo does support a List type that provides the necessary functionality for indexing, slicing, and iteration, the List type cannot be initialized using the same syntax as a list literal.

It would be great if Mojo's list literals could support the same operations as Python lists or if the List type could be initialized with the list literal syntax and also make sure that mojo listliteral supports nested list like python nested list

@melodyogonna
Copy link

What is the plan for ListLiteral in general?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

6 participants