mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[BUG]: Unable to get Tuple values

Open cheburakshu opened this issue 1 year ago • 5 comments

Bug description

for i in range(len(b)):
    print(i)

Prints:

0
1

Trying to get the values per doc: https://docs.modular.com/mojo/stdlib/builtin/tuple.html#get

for i in range(len(b)):    
    print(b.get[i, Int32]())

Error:

error: Expression [61]:20:21: cannot use a dynamic value in call parameter
        print(b.get[i, Int32]())
                    ^

expression failed to parse (no further compiler diagnostics)

Steps to reproduce

  • Include relevant code snippet or link to code that did not work as expected.
  • If applicable, add screenshots to help explain the problem.
  • If using the Playground, name the pre-existing notebook that failed and the steps that led to failure.
  • Include anything else that might help us debug the issue.

System information

- What OS did you do install Mojo on ?
- Provide version information for Mojo by pasting the output of `mojo -v`
- Provide Modular CLI version by pasting the output of `modular -v`

cheburakshu avatar Sep 19 '23 01:09 cheburakshu

Cannot iterate:

    print(i)
error: Expression [71]:19:14: 'Tuple[Int, Int]' does not implement the '__iter__' method
    for i in b:
             ^

expression failed to parse (no further compiler diagnostics)


Cannot index:

print(b[0])

Error:

error: Expression [72]:19:12: 'Tuple[Int, Int]' is not subscriptable, it does not implement the `__getitem__`/`__setitem__` methods
    print(b[0])
          ~^

expression failed to parse (no further compiler diagnostics)

cheburakshu avatar Sep 19 '23 01:09 cheburakshu

In fact the same bugs exists on ListLiteral! (mojo 0.3.1).

guidorice avatar Oct 05 '23 14:10 guidorice

With version mojo 0.5.0 (6e50a738) it gives cannot use a dynamic value in call parameter error when I try to get an item with an index parameter. But static index parameter works well.

This works:

var elements: Tuple[AnyType]
elements.get[0, AnyType]()

but this doesn't

fn __getitem__(self, index: Int) -> AnyType:
    return self.elements.get[index, AnyType]()

abdullahselek avatar Nov 19 '23 10:11 abdullahselek

I believe the fundamental issue here is:

generic types currently only work when bound with register_passable types (src: https://github.com/modularml/mojo/issues/523 )

So after traits are implemented, I imagine this issue and bunch of related ones will get fixed.

guidorice avatar Nov 19 '23 23:11 guidorice

We've seen massive improvements here, but basically the issue is that (x, y) should return a different type when x and y are homogenous vs heterogenous. In the python case, they are homogenous which allows indexing.

lattner avatar Apr 09 '24 02:04 lattner