lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.51k stars 164 forks source link

Implement generics #154

Open certik opened 2 years ago

certik commented 2 years ago

We need to allow writing generic functions (templates) and template constraints.

One idea that we can explore is to also support something like: https://github.com/lcompilers/lpython/pull/152#issuecomment-1047082741:

>>> def str(val: i32 | f64):
...   # "isinstance" type guard
...   if isinstance(val, i32):
...     # type of `val` is narrowed to `i32`
...     return str_int(val)
...   elif isinstance(val, f64):
...     # else, type of `val` is narrowed to `f64`
...     return str_float(val)
Smit-create commented 2 years ago

This should now be possible with overload decorator.

certik commented 2 years ago

This particular case can be. We still need generics/templates.

The overload in a sense is a subset of generics: you provide specific implementations ahead of time. The templates do not provide specific implementations, the compiler instantiates them.

Aser-Abdelfatah commented 2 months ago

Was this picked up by any person? Or is it still open for contribution?

certik commented 2 months ago

Yes, it's still open. We have quite solid generics in libasr thanks to LFortran, and we should hook them into LPython.

Aser-Abdelfatah commented 3 weeks ago

Hi, I'm doing more research into this. I found some generics implemented in GSoC 2022. I'd just appreciate if there were more details on which generics have higher priority or where to start

certik commented 3 weeks ago

They are implemented in LFortran, you can start here: https://docs.lfortran.org/en/asr/generics/.