lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.5k stars 157 forks source link

Returning tuples? #2495

Closed rebcabin closed 7 months ago

rebcabin commented 7 months ago

Works in CPython, fails LPython with an error message.

def foo() -> (int,):
    return (42,)
(lp) ┌─(~/Documents/GitHub/lpython/integration_tests)────(brian@MacBook-Pro:s001)─┐
└─(13:10:07 on vector-backend ✹ ✭)──> lpython ../ISSUES/UNHANDLED-EXCEPTIONS/Issue2495.py                                              2 ↵ ──(Tue,Feb06)─┘
semantic error: Return variable must be an identifier (Name AST node) or an array (Subscript AST node)
 --> ../ISSUES/UNHANDLED-EXCEPTIONS/Issue2495.py:1:14
  |
1 | def foo() -> (int,):
  |              ^^^^^^ 
Shaikh-Ubaid commented 7 months ago

We need to use tuple[i32]. For example:

% cat examples/expr2.py   
from lpython import i32

def foo() -> tuple[i32]:
    return (42,)

def main():
    print(foo())

if __name__ == "__main__":
   main()
% python examples/expr2.py
(42,)
% lpython examples/expr2.py
(42)

Example for returning multiple values:

% cat examples/expr2.py   
from lpython import i32

def foo() -> tuple[i32, bool]:
    return (42, True)

def main():
    print(foo())

if __name__ == "__main__":
   main()
% python examples/expr2.py 
(42, True)
% lpython examples/expr2.py
(42, True)
rebcabin commented 7 months ago

thanks !