Adding more generic examples (7th week)

Published: 07/31/2022

What I did this week

- Generic array types

I added support for generic array types. In the following simple example, T[:] signifies the type of one-dimensional array T.

T = TypeVar('T')

def f(lst: T[:], i: T) -> T:
    lst[0] = i
    return lst[0]

It is then possible to have different function calls involving different types of arrays:


To implement this, the type substitution checks between the formal parameter lst: T[:] and the argument [1] (i32[:]) match the type parameter T with i32 and to also check that the dimensions are equal.

- Merging TemplateFunction with Function

To distinguish generic functions from functions, I added a new syntactic construct TemplateFunction into LPython's intermediate representation ASR. However, this construct appears to be unnecessary and can be handled by the original Function construct by adding another field where type parameters are stored.

On a separate PR, I removed TemplateFunction from the grammar and modified the definition for Function.

What is coming up next week

There are tests that the compiler does not yet pass. So I will have to solve those first.

Did I get stuck anywhere

There was no issue specifically this week.