codon icon indicating copy to clipboard operation
codon copied to clipboard

Suggested feature specialize typing for small lists with constant indexes

Open jplevyak opened this issue 1 year ago • 1 comments

a = [1, "asdf", 2.0]
a[0] = 4
print a[0]

Unfortunately sometimes folks use small lists when they really should be using tuples, so the above code doesn't work.

However, these can be recognized and converted into tuples and given your bi-directional IR this shouldn't be too hard.

I do this in https://github.com/jplevyak/pyc by global constant propagation and specialization.

jplevyak avatar Mar 12 '23 00:03 jplevyak

They general way we're planning to handle cases like this is by promoting the list's type to a union, e.g. Union[int, str, float] in your case. Codon does support unions already, but doesn't yet do this automatic promotion. In certain cases we could also potentially just convert the list to a tuple as you mentioned.

arshajii avatar Mar 12 '23 13:03 arshajii