What I did this week
1. Adding more types of hard-coded restrictions
Again, continuing from last week's work, I added more types of restrictions for type parameters.
Currently there are four restrictions: SupportsZero, SupportsPlus, Divisible, Any. These restrictions are hard-coded into the compiler's frontend, meaning that each restriction is essentially associated with specific basic types available in LPython. For example, a type parameter restricted to SupportsZero has to be of type Integer or Real.
With these restrictions, it is possible to have the following generic function:
T = TypeVar('T', bound=SupportsPlus|SupportsZero|Divisible) def mean(x: list[T]) -> f64: k: i32 = len(x) if k == 0: return 0.0 sum: T sum = 0 i: i32 for i in range(k): sum = sum + x[i] return sum/k print(mean([1,2,3])) print(mean([1.0,2.0,3.0]))
Given the restrictions, any function instantiation is only required to check against the generic function's signature.
2. Handling ASR changes
The ASR design was recently changed so that Subroutine (function without return values) is merged into Function. Therefore I had to make modifications when adding generic functions and subroutines into the symbol table. This was done in PR #957 and has been subsequently merged.
What is coming up next week
1. Finalize the current hard-coded restrictions and have it merged
2. I did not have the time to handle arrays last week, so hopefully I can work on adding more support for generic arrays next week
Did I get stuck anywhere
This week was smooth sailing, although it took some iterations to have a clean pull request for supporting generic subroutines.