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

Bug:List to tuple conversion #573

Open
jordi-petit opened this issue Aug 1, 2024 · 4 comments
Open

Bug:List to tuple conversion #573

jordi-petit opened this issue Aug 1, 2024 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@jordi-petit
Copy link

Dear developers,

It seems to me that Codon is unable to perform a simple conversion from a list to a tuple. Here is a minimal example:

l = [1, 2]
t = tuple[int, int](l)
print(t)

The Python output is obviously (1, 2), but Codon 0.16.3 complains with this error:

b.py:2:5-23: error: 'Tuple[int,int]' object has no method '__new__' with arguments (List[int])

This is a major nuissance as, many times, lists must be converted to tuples to insert them into dicts.

@avitkauskas
Copy link

From Codon stdlib source I see it has DynamicTuple that can be initialized from the list (Codon 0.17.0).

On the other side, lists in Codon are hashable (with the hash based on all the values in the list), so they can be used as keys in maps. Any other list that is equal to the key list in the map will match.

lst = [1,2,3]
tup = DynamicTuple(lst)

print(lst)
print(tup)

map_lst_key = {lst: 1}
map_tup_key = {tup: 1}

print(map_lst_key)
print(map_tup_key)

output

[1, 2, 3]
(1, 2, 3)
{[1, 2, 3]: 1}
{(1, 2, 3): 1}

@jordi-petit
Copy link
Author

Thanks for your answer and mentioning DynamicTuple and lists as hashable keys, which I did not know about.

However, DynamicTuple is a Codon only feature not available in regular Python. It would still be nice to have a way to convert homogenous lists to tuples to improve compatibility.

@avitkauskas
Copy link

From the documentation (https://docs.exaloop.io/codon/general/differences):

  • Tuples: Since tuples compile down to structs, tuple lengths must be known at compile time, meaning you can't convert an arbitrarily-sized list to a tuple, for instance.

That's why DynamicTuple exists, and why no convertion of lists to tuples.

@inumanag
Copy link
Contributor

Hello @jordi-petit

DynamicTuple is not yet intended for use in production.

I will need to add special constructors for this; should be done in the next release.

@inumanag inumanag self-assigned this Sep 23, 2024
@inumanag inumanag added the bug Something isn't working label Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants